1. Home
  2. WC Vendors Pro
  3. Code Snippets
  4. Conditionally disable widgets on vendor store pages

Conditionally disable widgets on vendor store pages

We have a number of widgets that will only load on the vendor store pages. However there is a time when you might want to hide widgets on the vendor store pages. 

The following code will allow you to disable widgets on the vendor pages but leave them display on other pages. 

You will need to know the internal name of the sidebar you want to target. For storefront theme the sidebar internal id is ‘sidebar-1’. Your theme may be different. You can find the id in the register_sidebar() definition within your theme. 

The next thing you’ll need is the widget id(s) that you want to disable. You can find these easily by viewing the source code for a particular widget. The CSS id is the id we’re after.

Now that you have these two pieces of information. You can update the snippet below to reflect your particular theme and widget requirements.

/**	
 * Remove widgets from the vendor store and single product pages conditionally. 
 *  
 * You will need the sidebar internal id (eg sidebar-1) 
 * You will need the list of widget id's you want to disable (eg woocommerce_layered_nav-2, meta-2)
 */

add_filter( 'sidebars_widgets', 'wcv_woocommerce_conditionally_hide_widget' );
function wcv_woocommerce_conditionally_hide_widget( $sidebars_widgets ) {
	global $post; 

	$sidebar = 'sidebar-1'; // Internal reference for the sidebar
	$widgets = array( 'woocommerce_layered_nav-2' ) ; // Array of widget IDs to disable on the vendor pages. 

	if ( ! $post ) {
		return $sidebars_widgets;
	}

    if( ! is_admin() ) {
		// If its the vendor store page or single vendor product unload the widget 
		if ( WCV_Vendors::is_vendor_page() && WCV_Vendors::is_vendor_product_page( $post->post_author ) ) {
			foreach ($widgets as $widget ) {
				$key = array_search( $widget, $sidebars_widgets[$sidebar] );
				if( $key ) {
					unset( $sidebars_widgets[$sidebar][$key] );
				}	
			}
		}
    }
    return $sidebars_widgets;
}

filters

Updated on April 8, 2021

Was this article helpful?