Adding classes to WooCommerce widgets
-
I am using several attribute filter widgets on my shop page. I would like to programmatically add unique classes to each of these widgets.
1. Is there a filter that I could use to do this?
2. If not, how would I do it? I heave already overridden another function as follows:
class Doublee_WC_Widget_Layered_Nav extends WC_Widget_Layered_Nav { protected function layered_nav_list( $terms, $taxonomy, $query_type ) { // My custom layered_nav_list function, works as intended } }I can see the widget classes are located in the
WC_Widget_Layered_Navclass’s constructor:public function __construct() { $this->widget_cssclass = 'woocommerce widget_layered_nav woocommerce-widget-layered-nav'; $this->widget_description = __( 'Display a list of attributes to filter products in your store.', 'woocommerce' ); $this->widget_id = 'woocommerce_layered_nav'; $this->widget_name = __( 'Filter Products by Attribute', 'woocommerce' ); parent::__construct(); }I am new to object-oriented PHP and haven’t been able to work out how to override this. Simply duplicating the constructor in my custom class and changing the value of
$this->widget_cssclassdoesn’t have any effect (I’ll worry about making them unique later…I just need my custom class names to apply first!)Any advice much appreciated!
-
This topic was modified 8 years, 2 months ago by
doubleedesign.
-
This topic was modified 8 years, 2 months ago by
doubleedesign.
-
This topic was modified 8 years, 2 months ago by
doubleedesign.
-
This topic was modified 8 years, 2 months ago by
doubleedesign.
-
This topic was modified 8 years, 2 months ago by
-
Could I ask why you need to add classes? Each widget in WordPress already get’s a unique ID / class if you take a look at the markup.
Thanks for the quick reply!
The unique part is the incremented ID of
woocommerce_layered_nav-[number], whereas I need to target a specific widget, not its position (e.g. Attribute X filter widget, not the 6th filter widget). There is always a chance the client will reorder the widgets and the one I’m targeting with CSS is currently#woocommerce_layere_nav-6but there’s no guarantee it always will be.-
This reply was modified 8 years, 2 months ago by
doubleedesign.
Maybe worth giving this a try: https://ww.wp.xz.cn/plugins/widget-css-classes/
Otherwise, this hook should give you access to what you need: https://developer.ww.wp.xz.cn/reference/hooks/dynamic_sidebar_params/
Thanks, that hook should do the trick! 🙂
-
This reply was modified 8 years, 2 months ago by
The topic ‘Adding classes to WooCommerce widgets’ is closed to new replies.