覆盖 WooCommerce 类别列表 Walker 的最佳方式
Best way to override the WooCommerce category list Walker
覆盖 woocomerce 插件目录中定义的 woocomerce 函数的最佳方法是什么 class.
例如,我想更改 class WC_Product_Cat_List_Walker
中声明的函数 start_el
。在我升级 woocomerce 插件之前,我可以通过更改保留更改插件目录中的功能代码。
如何在我的主题 functions.php
文件中覆盖它?
试试这个...
您可以创建 class 并扩展到 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>';
}
}
}
然后像这样使用它:
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;
}
首先,只需尝试将 /includes/walkers/class-product-cat-list-walker.php
复制到您的主题中并包含新文件。它 可能 是可插入的,您可以直接在那里简单地编辑它而无需任何其他努力。我不是 100% 确定。
如果失败,那么您可以通过过滤 woocommerce_product_categories_widget_args
并提供您自己的助行器来完成,而不是尝试编辑 WooCommerce 的助行器。
将复制的 walker 文件保留在原处,但将所有 class 名称从 WC_Product_Cat_List_Walker
重命名为 SO_Product_Cat_List_Walker
。
然后在您的主题 functions.php
中告诉小部件使用这个新的 class 作为合适的 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' );
覆盖 woocomerce 插件目录中定义的 woocomerce 函数的最佳方法是什么 class.
例如,我想更改 class WC_Product_Cat_List_Walker
中声明的函数 start_el
。在我升级 woocomerce 插件之前,我可以通过更改保留更改插件目录中的功能代码。
如何在我的主题 functions.php
文件中覆盖它?
试试这个...
您可以创建 class 并扩展到 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>';
}
}
}
然后像这样使用它:
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;
}
首先,只需尝试将 /includes/walkers/class-product-cat-list-walker.php
复制到您的主题中并包含新文件。它 可能 是可插入的,您可以直接在那里简单地编辑它而无需任何其他努力。我不是 100% 确定。
如果失败,那么您可以通过过滤 woocommerce_product_categories_widget_args
并提供您自己的助行器来完成,而不是尝试编辑 WooCommerce 的助行器。
将复制的 walker 文件保留在原处,但将所有 class 名称从 WC_Product_Cat_List_Walker
重命名为 SO_Product_Cat_List_Walker
。
然后在您的主题 functions.php
中告诉小部件使用这个新的 class 作为合适的 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' );