Create custom Notice-Type, and use it on specific actions(i.e add to cart button)/pages in woommerce

I wish to create a custom “notice-type” in Woocommerce.

The default templates used for messages are: ‘Success’, ‘Error‘ & ‘Info‘.

Read More

I have created a Template in the Override folder of my theme -> woocommerce/notices/”my-notice.php”

How should I override, in my theme “functions.php” file the default call for ‘Success‘ notice-type in product-page-> add-to-cart button after submit, to use the custom notice-type->”my-notice.php” ?

Related posts

2 comments

  1. I know this answer is even later but I have found the exact solution you are looking for: a custom notice (type) for the add_to_cart action.
    I couldn’t find it elsewhere so dropping it here just in case.

    Creating the notice

    First, let WooCommerce know you have a new notice_type available:

    add_filter('woocommerce_notice_types', function($notice_types) {
      $notice_types[] = 'mynotice';
      return $notice_types;
    }, 10, 3);
    

    Second, create the notice template file in /woocommerce/notices/mynotice.php
    For example, copy the success notice and rename it and change the HTML a bit:

    <?php foreach ( $notices as $notice ) : ?>
    <div class="mycustomnotice" <?php echo wc_get_notice_data_attr( $notice ); ?> role="alert">
      <?php echo wc_kses_notice( $notice['notice'] ); ?>
    </div>
    <?php endforeach; ?>
    

    Now you can use your custom notice (anywhere) with the following code, just make sure the WC woocommerce_output_all_notices action has been hooked.

    wc_add_notice( 'my message here', 'mynotice' ); 
    // parameters: message html, notice type, notice data attributes
    

    Using custom notice for the add_to_cart action (overwrite)

    So now we have our notice working, start looking for the correct filter to alter the default message (in this case the add-to-cart message).
    Found here: WooCommerce Github v4.0, so let’s change it:

    add_filter('wc_add_to_cart_message_html', function ($message, $products, $show_qty) {
      return '<p> my custom notice html </p>';
    }, 10, 3);
    

    Or more advanced using a custom php template:

    add_filter('wc_add_to_cart_message_html', function ($message, $products, $show_qty) {
        ob_start();
        wc_get_template('mycustom/path/to/mynoticehtml.php');
        $notice_html = ob_get_clean();
        return $notice_html;
    }, 10, 3);
    

    All done! Cheers.

    EDIT:
    If you just want this specific notice for a one time use you can skip the message_html filter and simply drop your message in the mynotice.php template. (less dynamic though)

  2. I know this is a waaay late answer, but!
    Try adding:

    add_filter('woocommerce_notice_types', function ($notice_types) {
        $notice_types[] =   'warning'; // your custom type = custom template name
        return $notice_types;
    });
    

    to your function.php. This will let WC add your custom type to the “notices-to-be-printed” list.

    Then use it as usual:

    wc_add_notice("my new notice type here!", 'warning');
    

    BTW, i would avoid special characters in your custom type name.
    BTW! actually i’m not able to make it behave exactly like other types: i.e, when i update cart quantities, my custom notice-type message is not updated, while the original types are (?).

    EDIT
    Wow, i think i partially misnderstood your question..
    Maybe the best way to achieve your goal is via CSS (if you want to change aspect) or using one of these hooks: woocommerce_add_message and 'woocommerce_add_' . $notice_type (if you want to modify/filter the message).

Comments are closed.