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        businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 3.7
 * @community     https://businessbloomer.com/club/
 */

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 custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

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. Follow @rmelogli

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? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. 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 the Business Bloomer Club to get quick WooCommerce support. Thank you!

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