WooCommerce: Conditionally Hide Widgets

You could use a popular plugin called Widget Logic, or instead you could keep it simple with a few lines of PHP. Here’s a snippet for you in case you need to conditionally hide a certain sidebar widget given a condition e.g. if you’re on the Cart page.

Of course, you can use any of the available WooCommerce conditional tags and make this more complex, but in this example we’ll keep it simple and check if we’re looking at the Cart page (thanks to the is_cart() conditional). Enjoy!

The “woocommerce-products-2” widget ID that I want to hide on the WooCommerce Cart page

PHP Snippet: Remove a Sidebar Widget if @ WooCommerce Cart Page

Note: you’ll need to find the sidebar ID (go to the WordPress dashboard > “Appearance” > “Widgets” and right click on the sidebar in question. Click Inspect on Google Chrome and find the sidebar ID) and the widget ID (see screenshot above for 1 way to do that: right click on the widget, click Inspect on Google Chrome, and find the widget DIV ID).

In regard to my sample snippet below, I found out the sidebar ID is ‘sidebar-1‘ and the widget ID I wanted to hide is ‘woocommerce_products-2‘. You need to make sure you’re targeting the correct sidebar/widget or the code won’t work.

/**
 * @snippet       Hide Sidebar Widget @ WooCommerce Cart
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 3.7
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

add_filter( 'sidebars_widgets', 'bbloomer_woocommerce_conditionally_hide_widget' );

function bbloomer_woocommerce_conditionally_hide_widget( $sidebars_widgets ) {
    if( ! is_admin() ) {
		if ( is_cart() ) {
				$key = array_search( 'woocommerce_products-2', $sidebars_widgets['sidebar-1'] );
				if( $key ) {
					unset( $sidebars_widgets['sidebar-1'][$key] );
			}
		}
    }
    return $sidebars_widgets;
}

Where to add custom code?

You should place PHP snippets at the bottom of your child theme functions.php file and CSS at the bottom of its style.css file. Make sure you know what you are doing when editing such files - if you need more guidance, please take a look at my guide "Should I Add Custom Code Via WP Editor, FTP or Code Snippets?" and my video tutorial "Where to Place WooCommerce Customization?"

Does this snippet (still) work?

Please let me know in the comments if everything went as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting.

If you think this code saved you time & money, feel free to join 17,000+ WooCommerce Weekly subscribers for blog post updates and 250+ Business Bloomer supporters for 365 days of WooCommerce benefits. Thank you in advance!

Need Help with WooCommerce?

Check out these free video tutorials. You can learn how to customize WooCommerce without unnecessary plugins, how to properly configure the WooCommerce plugin settings and even how to master WooCommerce troubleshooting in case of a bug!

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza.

4 thoughts on “WooCommerce: Conditionally Hide Widgets

  1. Hi:

    Thanks for your snippet, it’s really useful. But I’m wondering if it’s possible to do the same not with a specific page but with a product category page (or more than one), and single products pages inside that category.

    1. Hi Inex I suggest you take a look at “conditional logic”: https://businessbloomer.com/woocommerce-conditional-logic-ultimate-php-guide/. Enjoy ๐Ÿ™‚

  2. Hi โ€“ Thanks a lot for this code and all your effort.
    regards

    1. Thanks a lot!

Questions? Feedback? Support? Leave your Comment Now!
_____

If you are writing code, please wrap it between shortcodes: [php]code_here[/php]. Failure to complying with this (as well as going off topic, not writing in English, etc.) will result in comment deletion. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining BloomerArmada to get blog comment reply priority, ask me 1-to-1 WooCommerce questions and enjoy many more perks. Thank you :)

Your email address will not be published. Required fields are marked *