Custom WordPress Notices With wc_add_notice Function

WooCommerce wc_add_notice Function

When customizing your WordPress website that uses the WooCommerce plugin, you may find yourself in a situation where you want to add a custom notice to the user. This can be done with the help of wc_add_notice function.
Let’s take a closer look at this function works and how we can use this function in our code.

wc_add_notice Function Parameters

The wc_add_notice($message, $notice_type = ‘success’, $data = []) function accepts 3 parameters:

  • $message (required) – This is the notice message that you want to display.
  • $notice_type (optional) – The type of a notice message that you want to display. By default, the type will be set to success, but you can also use error and notice as values for this parameter.
  • $data (optional) – additional data that can be passed to the notice. It can be useful if you want need extra parameters when displaying the notice.

How To Use The wc_add_notice Function

Example #1. Displaying a notice message to users who aren’t logged in, because logged in users can see hidden products :

<?php

add_action('init', function() {
    if (! is_user_logged_in()) {
        wc_add_notice('If you have an account, please log in to be able to see hidden products', 'notice');
    }
});

Example #2: Performing custom validation before user adds a product to their shopping cart:

<?php

add_action('woocommerce_add_to_cart_validation', function($passed, $product_id, $quantity, $variation_id = null, $variations = null) {
    // Don't allow guests to add this product to the cart
    if (! is_user_logged_in() && $product_id == 123) {
        wc_add_notice('Please log in to be able to add this product to your cart.', 'error');
        $passed = false;
    }
    
    return $passed;
}, 10, 5);

How To Modify Templates That wc_add_notice Function Uses?

The wc_add_notice function uses the following templates to render notices:

  • templates/notice/error.php
  • templates/notice/notice.php
  • templates/notice/success.php

If you want to override them, just copy and paste the appropriate error message template to your child theme. Assuming your child theme’s name is yourtheme and you want to override the error template, you would copy the templates/notice/error.php file to yourtheme/woocommerce/notices/error.php and then you edit that file safely.

Other Functions Similar To wc_add_notice Function

There are other similar functions that can be handy when you’re working with wc_add_notice function:

  • wc_has_notice($message, $notice_type = ‘success’) – checks if notice has already been added.
  • wc_notice_count($notice_type = ”) – checks how many notices are there. If the $notice_type parameter is passed, it will only count notices of that specific type.
  • wc_print_notice($message, $notice_type = ‘success’, $data = []) – prints a single notice.
  • wc_get_notices($notice_type = ”) – gets all notices. If $notice_type parameter is passed, it will only return the notices for that given type.
  • wc_print_notices($return = false) – will print or return all notices. If $return parameter is passed and set to true, it will return all rendered notices instead of printing them.
  • wc_clear_notices() – clears all notices from the session.

Conclusion

As you can see, the WooCommerce’s wc_add_notice function can be quite handy when you want to customize the way your e-commerce store works. Feel free to use it whenever you need to output a custom notice to the user.

About The Author

Andriy Haydash

Andriy Haydash

Andriy Haydash is a WordPress Expert who helps people build and launch successful WordPress membership and e-learning websites.

Note: Not all of the articles are written directly by me.
Affiliate Disclaimer: Some links in the post may be my affiliate links

The Ultimate Managed Hosting Platform

Before YOU Leave...
Join My Newsletter

Get practical tips & tricks on how to start, grow and market your course/membership site.