23

I'm trying to figure out where WooCommerce creates it's messages for when there is a success, error or notice in WooCommerce. I want to edit those messages to fit the scenario more neatly and also edit the HTML. Where are these messages located and how do I edit them?

Majo0od
  • 2,278
  • 10
  • 33
  • 58

5 Answers5

21

Many of them are directly in the plugin files - unfortunately. Some messages are tied to filter hooks that allow you to edit them without messing with plugin files but that's not always the case.

The message you wanted to change was "Product Name was successfully added to your cart". This one is set in the function wc_add_to_cart_message in wc-cart-functions.php and this function allows you to change it using a filter:

wc_add_notice( apply_filters( 'wc_add_to_cart_message', $message, $product_id ) );

So in your functions.php file you could add something like:

add_filter('wc_add_to_cart_message', 'handler_function_name', 10, 2);
function handler_function_name($message, $product_id) {
    return "Thank you for adding product" . $product_id;
}
Rahil Wazir
  • 10,007
  • 11
  • 42
  • 64
Yavor
  • 671
  • 3
  • 9
  • 5
    Correct, but instead of `apply_filters` you should use `add_filter` – Broshi Mar 12 '15 at 08:45
  • And it should be `add_filter( 'filter', 'function', 10, 2 );`, **10** being the priority and **2** the number of arguments (necessary to catch the `$product_id`. – brasofilo Nov 20 '15 at 13:41
  • Late, but if you want a more advanced customisation (eq custom HTML for the notice itself) see this one: https://stackoverflow.com/a/60971236/4668696 – Luuk Skeur Apr 01 '20 at 13:40
13

Open the plugin files and search for wc_add_notice:

This function has a filter:

apply_filters( 'woocommerce_add_' . $notice_type, $message );

The $notice_type is the second argument passed in all those occurrences.

Using something like this should work:

add_filter( 'woocommerce_add_error', function( $message ) {
    if( $message == 'Some message' )
        $message = '';

    return $message;
});
brasofilo
  • 25,496
  • 15
  • 91
  • 179
  • 2
    Just a heads up to you and anyone else interested. You can find the functions for notices in the wc-notice-functions.php file which is located in the includes folder. – Majo0od Oct 07 '14 at 12:03
  • How about messages that are a little more dynamically generated like "View Cart *Product Name* was successfully added to your cart" – Majo0od Oct 07 '14 at 12:08
  • I don't have any setup where I can test this. Did you try inspecting the messages that arrive at the filter? – brasofilo Oct 08 '14 at 00:48
  • I did. It didn't help – Majo0od Oct 14 '14 at 15:27
4

The filters mentioned here work fine for editing the message itself, but if you want to edit the actual HTML markup containing the notice message, then you need to use the notice templates under templates > notices.

There are three different files here, each for the different kinds of notices. In my case, I wanted to add a class to the coupon applied successfully notice, so I copied success.php over into my theme file. My code then looked like below:

<?php foreach ( $messages as $message ) : ?>
    <?php 
        $om_css_class = "";
        if ( $message == "Coupon code applied successfully." ) {
            $om_css_class = "coupon-notice-msg";
        } 
    ?>
    <div class="woocommerce-message <?php echo $om_css_class; ?>"><?php echo wp_kses_post( $message ); ?></div>
<?php endforeach; ?>
Ollie Murphy
  • 355
  • 3
  • 10
3

I came across this answer and was able to implement for a production site. This answer is related to woocommerce error codes notices. You need to find the codes in the separate class files (~woocommerce/includes/). For my purpose the code was in ~woocommerce/includes/class-wc-coupon.php

/**
 * Modify the coupon errors:
*/

 add_filter( 'woocommerce_coupon_error', 'wpq_coupon_error', 10, 2 );

 function wpq_coupon_error( $err, $err_code ) {
  return ( '103' == $err_code ) ? '' : $err;
 }

Thanks to this page: http://wpquestions.com/WooCommerce_Remove_Coupon_code_already_applied_error_message/10598

user3795286
  • 136
  • 1
  • 14
0

I did it for error.php file. file path is woocommerce/templates/notices/error.php

<ul class="woocommerce-error" role="alert">
    <?php
  foreach ( $messages as $message ) :

  if($message=="<strong>Billing Email address</strong> is a required field.") { $message="<strong>Email address</strong> is a required field."; }?>
        <li><?php echo wp_kses_post( $message ); ?></li>
    <?php endforeach; ?>
</ul>
LuFFy
  • 8,799
  • 10
  • 41
  • 59
developer
  • 9
  • 1