覆盖WooCommerce功能以扩展类别列表设计



我已经在class-wc-product-cat-list-walker.php文件中做了一个更改,以下是代码中的更改

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>';
}
}

我想将其保留在儿童主题中并使其正常运行。有什么办法吗?我尝试复制文件并将其粘贴到子主题WooCommerce文件夹中。但它不起作用。

请帮助我知道更改它的方法。提前谢谢。

您可以创建自己的自定义类并尝试覆盖WC_Product_Cat_List_Walker

所以你可以做这样的事情

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>';
}
}
}

然后你可以像这样调用该函数

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;
}

只需在您的活动主题函数中使用此代码.php文件并检查即可。

最新更新