How to hide specific messages in woocommerce?

2019-06-02 11:06发布

i want to hide/remove specific messages from woo commerce without modifying basic woocommerce plugin. There are several types of messages related to coupon like

  1. Coupon code already applied!
  2. Sorry! Coupon 12345 already applied to your cart. (here i essentially want to hide coupon code)

and several others similar to these coupon codes.

i just want to hide these type of coupon/cart messages, others are fine like "Product successfully added!" or any other error messages.

Basically the aim is to show all other messages (Error and success messages) but dont wanna show coupon messages and a coupon code in to these messages.

So, is there any way to do this by doing any hook etc, like one i've found to eliminate all message strings (if i am not wrong).

add_filter( 'woocommerce_coupon_message', '__return_empty_string' );
  1. One more thing is, one message is repeating on cart page several times when i add product in to the car. "Coupon code already applied!" 2,3, to 4 times.

3条回答
女痞
2楼-- · 2019-06-02 11:48

I know this is an old thread, but I think I have just worked out how to do this for the coupon error messages.

On my store I use WooCommerce Smart Coupons which allows coupons to be sold as gift cards. I don't want people to be able to purchase gift cards using a gift card, so I have added the gift card category to the exclude category list on the coupon usage restriction.

Anyway, I wanted to alter the error message if somebody tried to use a gift card coupon code when there was a gift card in the cart. This is the code I used:

function filter_woocommerce_coupon_error( $err, $err_code, $instance ) {

    if ( $err_code == '114' ) {

        global $woocommerce;                
        $categories = $instance->get_excluded_product_categories();

        if ( in_array( '15', $categories ) ) {

            $err = sprintf( __( 'Sorry, you cannot use a Gift Card to purchase another Gift Card.' ) );

        }
    }   

    return $err;

};

add_filter( 'woocommerce_coupon_error', 'filter_woocommerce_coupon_error', 10, 3 );

The $err_code for specific error messages can be found in the file woocommerce/includes/class-wc-coupon.php.

In my case, I wanted to edit error message for error code 114 (E_WC_COUPON_EXCLUDED_CATEGORIES). I also wanted my custom message to only appear if a 114 error was triggered by a gift card being in the cart, not for every 114 error. To solve this I added in the if ( in_array( '15', $categories ) ). What this does is check to see if the error message has been triggered by a product that is in category 15 (this being the gift card category for my store. Change 15 to whichever category you are using).

The $instance variable is what WooCommerce uses to pass the details of the cart/coupon back to the function.

I'm very new to coding, so I am sorry if my code and my explanation isn't great, but it definitely appears to work for me. I added it into functions.php.

查看更多
何必那么认真
3楼-- · 2019-06-02 11:52

So the crux of your question is - how can I customise WooCommerce coupon messages?

I have half an answer - I've customised coupon messages (the green boxed ones) using the "woocommerce_coupon_message" filter. BUT I've not yet been able to get the coupon error messages (the red boxed ones) working using the "woocommerce_coupon_error" filter.

I've tried conditional statements based on a few different methods from Class WC_Cart to no avail. I just cant seem to "intercept" (and then customise) the error messages before they're printed. If somebody has a solution to Coupon Errors I'd be happy to hear it.

Anyhow... the below function is hooked into the "woocommerce_before_cart" & "woocommerce_before_checkout_form" actions so the function works for either page.

It can obviously be customised to no end, but my example basically tests for a valid coupon and then you can change the message or return nothing. You can also test for other conditions to throw up custom notices of all sorts! Much better then modifying template files! :-)

add_action( 'woocommerce_before_cart', 'custom_coupon_messages' );
add_action( 'woocommerce_before_checkout_form', 'custom_coupon_messages' );
function custom_coupon_messages() {
    global $woocommerce;

    //Set coupon codes.
    $coupon_code = 'Bigly-Yuge';

    //Set coupon objects.
    $coupon_test = new WC_Coupon( 'Bigly-Yuge' );

    //Get the cart subtotal. Should return as a Double.
    $cart_subtotal = WC()->cart->subtotal;

    //If coupon test is passed add coupon.
    if ( $coupon_test->is_valid() && $woocommerce->cart->has_discount( $coupon_code ) ) {
        //Filter the coupon success message to display a custom message.
        add_filter( 'woocommerce_coupon_message', 'filter_woocommerce_coupon_message', 10, 3 );
        function filter_woocommerce_coupon_message( $msg, $msg_code, $instance ) {
            //Set a custom coupon message.
            $msg_code = 'case self::WC_COUPON_SUCCESS';
            $msg = __( 'You saved BIGLY YUGE!', 'woocommerce' );

            return $msg;
            return $msg_code;

            //Or return nothing (no message will be displayed - comment out the above/uncomment below).
            // return '';
        };
        //Print the above notice to screen.
        wc_print_notices();
    }
    elseif ( $cart_subtotal > 499 ) {
        //Print a notice (the blue boxed one)
        wc_print_notice( 'Spend $500 to qualify for the BIGLY YUGE discount!!!', 'notice' );
    }
}
查看更多
Evening l夕情丶
4楼-- · 2019-06-02 12:05

Okay, found solution

go to woocommerce tempaltes, copy notices folder and edit the desired template, in my case its error.php

copy/edit code

<ul class="woocommerce-error">
    <?php
     foreach ( $messages as $message ) : 
     if ( $message == "Coupon code already applied!" ) {
            $message = "";//empty error string

        }  else if (strpos($message, 'does not exist!') !== false) {
                $message = ""; //empty error string

            }
           else if (strpos($message, 'Sorry, it seems the coupon') !== false) {
                $message = "";//empty error string

            }
           else if (strpos($message, 'Sorry, this coupon is not applicable to your cart contents') !== false) {
                $message = "Sorry, the discount is not applicable to your cart contents"; //updated error string

            }
    ?> 
        <li><?php echo wp_kses_post( $message ); ?></li>
    <?php
    break;
     endforeach; ?>
</ul>
查看更多
登录 后发表回答