Conditionally display WooCommerce store notice

WooCommerce provides a useful option to display a message across every page/post  of your website. However, if you only want to display the notice on specific parts of your website then it will require a little bit of extra code.

In this example we are only going to display the store notice on pages related to the store. To do this we are going to use the conditional tags provided by WooCommerce.

Add the following code to your theme’s functions.php file:

function my_hide_notice() {
  if( function_exists('is_woocommerce') ) {
    remove_action( 'wp_footer', 'woocommerce_demo_store' );
    if( is_woocommerce() || is_cart() || is_checkout() ) { 
      add_action( 'wp_footer', 'woocommerce_demo_store' ); 
    } 
  } 
} 
add_action( 'wp', 'my_hide_notice' );
  1. We use function_exists to check if the WooCommerce plugin is currently active
  2. If it is then we remove the action that displays the notice
  3. We then use the conditional tags to check whether the current page is a single product, product archive, shopping cart or checkout
  4. If it is we use add_action to display the notice, at this point you could move the location in your theme the notice displays by changing wp_footer

Leave a Reply

Your e-mail address will not be published. Required fields are marked *