覆盖WooCommerce类别列表Walker的最佳方法

Meh*_*har 2 php wordpress wordpress-theming wordpress-plugin woocommerce

覆盖某个类的woocomerce plugins目录中定义的woocomerce函数的最佳方法是什么。例如,我想更改类中声明的函数。我可以通过更改保留更改插件目录中的功能代码,直到升级woocomerce插件为止。如何在主题文件中覆盖它?start_elWC_Product_Cat_List_Walkerfunctions.php

Rei*_*gel 5

尝试这个...

您可以创建一个类并扩展到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>';
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

然后像这样使用它:

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;
}
Run Code Online (Sandbox Code Playgroud)