Best way to override the WooCommerce category list Walker

What is the best way to override the woocomerce function that is in defined in the woocomerce plugins directory inside some class.
For example I am wanted to change the function start_el declared in the class WC_Product_Cat_List_Walker. I can change the function code in the plugin directory by the changes remains until I upgrade the woocomerce plugin.
How can I override it in my theme functions.php file?

Related posts

2 comments

  1. try this…

    you can create a class and extend to WC_Product_Cat_List_Walker.

    class My_WC_Product_Cat_List_Walker extends WC_Product_Cat_List_Walker {
        public $tree_type = 'product_cat';
        public $db_fields = array ( 'parent' => 'parent', 'id' => 'term_id', 'slug' => 'slug' );
    
        public function start_el( &$output, $cat, $depth = 0, $args = array(), $current_object_id = 0 ) {
            $output .= '<li class="cat-item cat-item-' . $cat->term_id;
    
            if ( $args['current_category'] == $cat->term_id ) {
                $output .= ' current-cat';
            }
    
            if ( $args['has_children'] && $args['hierarchical'] ) {
                $output .= ' cat-parent';
            }
    
            if ( $args['current_category_ancestors'] && $args['current_category'] && in_array( $cat->term_id, $args['current_category_ancestors'] ) ) {
                $output .= ' current-cat-parent';
            }
    
            $output .=  '"><a href="' . get_term_link( (int) $cat->term_id, $this->tree_type ) . '">' . __( $cat->name, 'woocommerce' ) . '</a>';
    
            if ( $args['show_count'] ) {
                $output .= ' <span class="count">(' . $cat->count . ')</span>';
            }
        }
    
    }
    

    then use it as like:

    add_filter('woocommerce_product_categories_widget_args', 'woorei_product_categories_widget_args', 10, 1);
    
    function woorei_product_categories_widget_args($args) {
        $args['walker'] = new My_WC_Product_Cat_List_Walker;
        return $args;
    }
    
  2. First, simply try copying /includes/walkers/class-product-cat-list-walker.php into your theme and include the new file. It may be pluggable and you can simply edit it there directly without any other effort. I’m not 100% sure.

    If that fails, then you can do it by filtering woocommerce_product_categories_widget_args and supplying your own walker instead of trying to edit WooCommerce’s.

    Leave your duplicated walker file in place, but rename all the class names from WC_Product_Cat_List_Walker to SO_Product_Cat_List_Walker.

    And then in your theme’s functions.php tell the widget to use this new class as the appropriate Walker.

    function so_32901408_cat_widget( $args ){
    
           // include new walker class - where ever you saved/named it
           include_once( 'wc-class-product-cat-list-walker.php' ); 
           // set the name as the new walker for the widget args
           $args['walker'] = new SO_Product_Cat_List_Walker;
           return $args;
    }
    add_filter( 'woocommerce_product_categories_widget_args', 'so_32901408_cat_widget' );
    

Comments are closed.