S
S
smash_wp2015-10-22 16:55:49
Web development
smash_wp, 2015-10-22 16:55:49

How to add class to WooCommerce category list?

How can I add a class to the category list widget? I found a file where all this is created (class-wc-widget-product-categories.php), but since this is a core file, it is clear that when woocommerce is updated, all changes will be lost. Perhaps this can be done using filters? Tell me please.
<ul class="product-categories">

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lee_Swagger, 2015-10-28
@Lee_Swagger

One way to solve the problem is to override the WC_Widget_Product_Categories class method.
To do this, create the file [current_theme]/widgets/widget-product_categories.php
Place the code of the overridden method there

class Custom_WC_Widget_Product_Categories extends WC_Widget_Product_Categories {
 
  function widget( $args, $instance ) {
    // скопировать весь код метода из:
    // woocommerce/includes/widgets/class-wc-widget-product-categories.php
    // добавить нужный класс в строке с выводом открывающего тега списка
    echo '<ul class="product-categories <b>my-class</b>">';
  }
}

Then place the following code in the functions.php of the active theme:
/**
 * WC_Widget_Product_Categories widget override
 */
add_action( 'widgets_init', 'override_woocommerce_widgets', 15 );

function override_woocommerce_widgets() {

  if ( class_exists( 'WC_Widget_Product_Categories' ) ) {
    unregister_widget( 'WC_Widget_Product_Categories' );
 
    include_once( 'widgets/widget-product_categories.php' );
 
    register_widget( 'Custom_WC_Widget_Product_Categories' );
  }

}

Here, the widget is re-registered with already changed parameters, while the core files do not change.
Actually, everything.
PS It is described in more detail here .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question