英文:
Woocommerce: Extend admin product list quick filters
问题
我需要扩展WooCommerce在管理产品视图中提供的快速筛选器,但似乎找不到适当的钩子来实现这一点,有人可以指点我正确的方向吗?
这是我想要扩展的视图,我想在其中添加两个链接,直接按某个类别筛选产品列表(通常深埋在类别下拉菜单中,所以客户希望在上面有一个一键解决方案):
我已经设置了一个自定义插件并尝试找到适当的钩子,但没有成功。
英文:
I need to extend the quick filters provided by Woocommerce in the admin product view but I cannot seem to find the proper hook to achieve this, can anyone point me in the right direction?
This is the view that I want to extend with two links that directly filter the product list by a certain category (which is regularly buried deep in the category dropdown so the customer wants it above to have a one click solution):
I've already set up a custom plugin and tried to find the proper hook but to no avail.
答案1
得分: 1
以下是代码的翻译部分:
// 这里是如何添加这些分类的 - https://prnt.sc/gFuhHx_BKupB
function add_custom_category_links( $views ) {
global $wp_query;
$taxonomy = 'product_cat';
$current_term = isset( $_GET[ $taxonomy ] ) ? sanitize_text_field( $_GET[ $taxonomy ] ) : '';
$current_status = isset( $_GET['post_status'] ) ? sanitize_text_field( $_GET['post_status'] ) : '';
// 更改为您想要添加的分类
$category_links = array(
'clothes' => '服装',
'shoes' => '鞋类',
);
$output = array();
foreach ( $views as $key => $view ) {
$output[ $key ] = $view;
if ( $key === 'publish' ) {
foreach ( $category_links as $slug => $name ) {
$class = ( $slug === $current_term ) ? 'current' : '';
$url = add_query_arg(
array(
$taxonomy => $slug,
'post_status' => $current_status,
'post_type' => 'product',
'paged' => 1,
),
admin_url( 'edit.php' )
);
$output[ 'category-' . $slug ] = "<a href='$url' class='$class'>$name</a>";
}
}
}
return $output;
}
add_filter( 'views_edit-product', 'add_custom_category_links' );
请注意,我已将分类的名称从英文翻译为中文。如果您需要进一步的帮助,请随时告诉我。
英文:
Here is how you can add these categories - https://prnt.sc/gFuhHx_BKupB
function add_custom_category_links( $views ) {
global $wp_query;
$taxonomy = 'product_cat';
$current_term = isset( $_GET[ $taxonomy ] ) ? sanitize_text_field( $_GET[ $taxonomy ] ) : '';
$current_status = isset( $_GET['post_status'] ) ? sanitize_text_field( $_GET['post_status'] ) : '';
//Change to categories you want to add
$category_links = array(
'clothes' => 'Clothes',
'shoes' => 'Shoes',
);
$output = array();
foreach ( $views as $key => $view ) {
$output[ $key ] = $view;
if ( $key === 'publish' ) {
foreach ( $category_links as $slug => $name ) {
$class = ( $slug === $current_term ) ? 'current' : '';
$url = add_query_arg(
array(
$taxonomy => $slug,
'post_status' => $current_status,
'post_type' => 'product',
'paged' => 1,
),
admin_url( 'edit.php' )
);
$output[ 'category-' . $slug ] = "<a href='$url' class='$class'>$name</a>";
}
}
}
return $output;
}
add_filter( 'views_edit-product', 'add_custom_category_links' );
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论