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
 * @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

  • WooCommerce Conditional Logic – Tags, Examples & PHP
    The WooCommerce and WordPress conditional tags (“WooCommerce and WordPress Conditional Logic”) can be used in your functions.php to display content based on certain conditions. For example, you could display different content for different categories within a single PHP function.
  • WooCommerce: Show Message Upon Country Selection @ Checkout
    If you have country-specific shipping conditions, or you want to show a message conditionally after country selection on the checkout page, here’s a simple WooCommerce PHP snippet you can freely use. This can be slightly customized to target state selection instead. I’ve also edited the initial code to make this work on load as well, […]
  • WooCommerce: Per-Product Terms & Conditions @ Checkout
    A freelance client sells two distinct products on the same website: a membership and an online course. Two different audiences, different formats and… different Terms & Conditions. The goal was therefore to display the “Terms & Conditions” checkbox on the Checkout page based on the product in the cart. Once again, we’re going to use […]
  • WooCommerce: How to Enable Catalog Mode?
    One of the most common WooCommerce questions is: can I use WooCommerce to build a catalog of products (without add to cart, price… basically a product gallery)? Using WooCommerce for this case scenario is indeed very helpful – you can make the most of all the inbuilt features such as single product gallery and carousel, […]
  • WooCommerce: Override Product Category Page Title
    This is an interesting WooCommerce customization – as you know WordPress menus and widgets read whatever product category name and display it in the frontend. Let’s say your product category title is “Tables”. This will show up in the navigation menu if you have set it up that way, in te breadcrumbs if you have […]

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 *