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!
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;
}
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.
Hi Inex I suggest you take a look at “conditional logic”: https://businessbloomer.com/woocommerce-conditional-logic-ultimate-php-guide/. Enjoy 🙂
Hi – Thanks a lot for this code and all your effort.
regards
Thanks a lot!