Search code examples
phpwordpresstemplateswoocommercehook-woocommerce

Override the WooCommerce function to extend category list design


I have done a change in the class-wc-product-cat-list-walker.php file and following is the changes in the code

public function start_el( &$output, $cat, $depth = 0, $args = array(), $current_object_id = 0 ) {
    $cat_id = intval( $cat->term_id );

    $output .= '<li class="cat-item cat-item-' . $cat_id;

    if ( $args['current_category'] === $cat_id ) {
        $output .= ' current-cat';
    }

    if ( $args['has_children'] && $args['hierarchical'] && ( empty( $args['max_depth'] ) || $args['max_depth'] > $depth + 1 ) ) {
        $output .= ' cat-parent';
    }

    if ( $args['current_category_ancestors'] && $args['current_category'] && in_array( $cat_id, $args['current_category_ancestors'], true ) ) {
        $output .= ' current-cat-parent';
    }
    $pageurl = wp_get_referer(); 
    $template = basename($pageurl);
    $post_id = get_the_ID();
    $catpage = get_field( 'catelog_page', $post_id );
    $catalogpage = 'no';
    if (isset($_GET['catalog'])) {
        $catalogpage = 'yes';
    }
    if (  $catpage == TRUE || $catalogpage=='yes' ) {
        $output .= '"><a href="' . get_term_link( $cat_id, $this->tree_type ) . '?catalog=true">' . apply_filters( 'list_product_cats', $cat->name, $cat ) . '</a>';
    }
    else {
        $output .= '"><a href="' . get_term_link( $cat_id, $this->tree_type ) . '">' . apply_filters( 'list_product_cats', $cat->name, $cat ) . '</a>';
    }


    if ( $args['show_count'] ) {
        $output .= ' <span class="count">(' . $cat->count . ')</span>';
    }
}

I want to keep it in the child theme and make it functional. Is there any way? I have tried to copy the file and paste it in the child theme WooCommerce folder. but it is not working.

please help me to know the way to change it. Thanks in advance.


Solution

  • You can create your own custom class and try to override WC_Product_Cat_List_Walker.

    So you can do something like this

    class Custom_WC_Product_Cat_List_Walker extends WC_Product_Cat_List_Walker {
        /**
         * What the class handles.
         *
         * @var string
         */
        public $tree_type = 'product_cat';
    
        /**
         * DB fields to use.
         *
         * @var array
         */
        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 ) {
            $cat_id = intval( $cat->term_id );
    
        $output .= '<li class="cat-item cat-item-' . $cat_id;
    
        if ( $args['current_category'] === $cat_id ) {
            $output .= ' current-cat';
        }
    
        if ( $args['has_children'] && $args['hierarchical'] && ( empty( $args['max_depth'] ) || $args['max_depth'] > $depth + 1 ) ) {
            $output .= ' cat-parent';
        }
    
        if ( $args['current_category_ancestors'] && $args['current_category'] && in_array( $cat_id, $args['current_category_ancestors'], true ) ) {
            $output .= ' current-cat-parent';
        }
        $pageurl = wp_get_referer(); 
        $template = basename($pageurl);
        $post_id = get_the_ID();
        $catpage = get_field( 'catelog_page', $post_id );
        $catalogpage = 'no';
        
        if (isset($_GET['catalog'])) {
            $catalogpage = 'yes';
        }
    
        if (  $catpage == TRUE || $catalogpage=='yes' ) {
            $output .= '"><a href="' . get_term_link( $cat_id, $this->tree_type ) . '?catalog=true">' . apply_filters( 'list_product_cats', $cat->name, $cat ) . '</a>';
        }
        else {
            $output .= '"><a href="' . get_term_link( $cat_id, $this->tree_type ) . '">' . apply_filters( 'list_product_cats', $cat->name, $cat ) . '</a>';
        }
    
    
        if ( $args['show_count'] ) {
            $output .= ' <span class="count">(' . $cat->count . ')</span>';
        }
        }
    
    }
    

    Then you can call that function like this

    add_filter('woocommerce_product_categories_widget_args', 'custom_product_categories_widget_args', 10, 1);
    
    function custom_product_categories_widget_args($args) {
        $args['walker'] = new Custom_WC_Product_Cat_List_Walker;
      return $args;
    }
    

    Just use this code in your active theme functions.php file and check.