How to enable WooCommerce Conditionally Hide Widgets function?

Sometimes, you need to hide certain functions to improve user interface. Such a hide is possible with a little PHP coding. In this article, you will learn about WooCommerce Conditionally Hide Widgets at cart page. 

In this example, we will apply simple coding. After that, we will make sure whether we can conditionally hide widgets or not. You must be able to remove sidebar widget if you are at the cart page. 

Image showing WooCommerce Conditionally Hide Widgets function

PHP Snippet: WooCommerce Conditionally Hide Widgets

Firstly, you should search for the sidebar ID. To get it, you can visit WordPress dashboard > Appearance > Widgets. After that, you should click on the sidebar. Click Inspect on Chrome browser and search for the sidebar ID.

Once done, you also need to find a widget ID. You can right click on the widget, press Inspect on chrome browser, & look for widget ID. 

In the below snippet, the sidebar ID is ‘sidebar-1’ and widget ID is ‘woocommerce-products-2’. You must search the right data as otherwise the code won’t work. 

add_filter( 'sidebars_widgets', 'phpsof_woocommerce_conditionally_hide_widget' );
 
function phpsof_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 do you add this snippet?

So, this is how you can enable WooCommerce Conditionally Hide Widgets at cart page. Also, it is easier to activate this function with this code. So, to apply this code, just add this PHP snippet at your child theme function.php file’s bottom. It should be placed before “?>”, if you have it there. Apart from this, CSS goes in your child theme style.css file.

Is this snippet still valid?

So, this way, you can learn about WooCommerce Conditionally Hide Widgets at cart pageI have applied this code on the Storefront theme and WordPress friendly hosting PHP 7.3. Let me know if everything works as expected. Share it further if you find this snippet useful for you and it had saved your time.

Also Read, How to Enable WooCommerce Redirect My Account Tab to URL function?

How to Add WordPress Customizer Setting in WooCommerce?

Important links: WooCommerce – https://woocommerce.com/