Display a custom notice in woocommerce

Picture of Amitpal Singh
Amitpal Singh
April 26, 2023

Display custom notice in woocommerce

Quick Solution:
/* custom notices */
add_action('template_redirect', 'my_custom_message');
function my_custom_message() {
//you can change role or page slug below
    if (current_user_can('subscriber') && is_page('my-account') ) {
        wc_add_notice( sprintf( __('You are not an <strong>"administrator"</strong> so can not access order form…'), get_permalink( get_option('woocommerce_myaccount_page_id') ) ), 'notice' );
    }
} 

WooCommerce is one of the most popular eCommerce plugins for WordPress. It allows you to create a fully-functional online store with ease. Custom notices are an essential part of any eCommerce store. They help to communicate important information to customers, such as order status, shipping information, and product updates. In this article, we will discuss how to display custom WooCommerce notices on a page.

What are WooCommerce Notices?

WooCommerce notices are messages that are displayed to customers during various stages of the purchasing process. They can be used to communicate important information, such as successful order placement, payment confirmation, or order cancellation. By default, WooCommerce comes with several pre-built notices, but you can also create your own custom notices to better suit your store’s needs.

Displaying Custom WooCommerce Notices on a Page

Displaying custom WooCommerce notices on a page is a simple process that involves two main steps. First, you need to create a custom notice. Second, you need to display the notice on a page.

Step 1: Creating a Custom Notice

Creating a custom WooCommerce notice involves using PHP code to define the notice’s content and display options. Here is an example of a simple custom notice that displays a message to the customer:

add_action( 'woocommerce_before_checkout_form', 'custom_checkout_notice' );
function custom_checkout_notice() {
    wc_print_notice( 'This is a custom checkout notice', 'notice' );
}
 

This code adds a custom notice to the WooCommerce checkout page that displays a message to the customer. The wc_print_notice() function is used to display the notice. The first parameter specifies the notice’s content, and the second parameter specifies the notice type (e.g., ‘notice’, ‘error’, ‘success’, etc.).

Step 2: Displaying the Custom Notice on a Page

Once you have created your custom notice, the next step is to display it on a page. This involves using the appropriate WooCommerce action hook to insert the notice into the page’s content. Here is an example of how to display the custom checkout notice on the WooCommerce cart page:

add_action( 'woocommerce_before_cart_table', 'custom_cart_notice' );
function custom_cart_notice() {
    wc_print_notice( 'This is a custom cart notice', 'notice' );
} 

In this example, the woocommerce_before_cart_table action hook is used to display the custom notice before the cart table. The custom_cart_notice() function is used to define the content of the notice.

Conclusion

Custom WooCommerce notices are an essential part of any eCommerce store. They allow you to communicate important information to your customers and improve their overall shopping experience. Creating and displaying custom notices on a page is a simple process that involves using PHP code and WooCommerce action hooks. By following the steps outlined in this article, you can create and display custom notices on any page in your WooCommerce store.

Share this post:

How to Attribute?

Lorem ipsum is typically a corrupted version of De finibus bonorum et malorum, a 1st-century BC text by the Roman statesman and philosopher Cicero.
for Example: Website, Social Media, Blogs, ebooks , newsletter, etc.
Lorem ipsum is typically a corrupted version of De finibus bonorum et malorum, a 1st-century BC text by the Roman statesman and philosopher Cicero.
Copied!

Got a Question? Check out our FAQ Section.

Your action, our appreciation

It encourage us to give you more valuable content on website.