Woocommerce: 扩展管理员产品列表快速筛选

huangapple go评论69阅读模式
英文:

Woocommerce: Extend admin product list quick filters

问题

我需要扩展WooCommerce在管理产品视图中提供的快速筛选器,但似乎找不到适当的钩子来实现这一点,有人可以指点我正确的方向吗?

这是我想要扩展的视图,我想在其中添加两个链接,直接按某个类别筛选产品列表(通常深埋在类别下拉菜单中,所以客户希望在上面有一个一键解决方案):

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):

Woocommerce: 扩展管理员产品列表快速筛选

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       = &#39;product_cat&#39;;
    $current_term   = isset( $_GET[ $taxonomy ] ) ? sanitize_text_field( $_GET[ $taxonomy ] ) : &#39;&#39;;
    $current_status = isset( $_GET[&#39;post_status&#39;] ) ? sanitize_text_field( $_GET[&#39;post_status&#39;] ) : &#39;&#39;;

    //Change to categories you want to add
    $category_links = array(
        &#39;clothes&#39; =&gt; &#39;Clothes&#39;,
        &#39;shoes&#39;   =&gt; &#39;Shoes&#39;,
    );

    $output = array();
    foreach ( $views as $key =&gt; $view ) {
        $output[ $key ] = $view;
        if ( $key === &#39;publish&#39; ) {
            foreach ( $category_links as $slug =&gt; $name ) {
                $class = ( $slug === $current_term ) ? &#39;current&#39; : &#39;&#39;;

                $url = add_query_arg(
                    array(
                        $taxonomy     =&gt; $slug,
                        &#39;post_status&#39; =&gt; $current_status,
                        &#39;post_type&#39;   =&gt; &#39;product&#39;,
                        &#39;paged&#39;       =&gt; 1,
                    ),
                    admin_url( &#39;edit.php&#39; )
                );

                $output[ &#39;category-&#39; . $slug ] = &quot;&lt;a href=&#39;$url&#39; class=&#39;$class&#39;&gt;$name&lt;/a&gt;&quot;;
            }
        }
    }

    return $output;
}

add_filter( &#39;views_edit-product&#39;, &#39;add_custom_category_links&#39; );

huangapple
  • 本文由 发表于 2023年6月5日 16:17:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76404604.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定