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

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

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

以下是代码的翻译部分:

  1. // 这里是如何添加这些分类的 - https://prnt.sc/gFuhHx_BKupB
  2. function add_custom_category_links( $views ) {
  3. global $wp_query;
  4. $taxonomy = 'product_cat';
  5. $current_term = isset( $_GET[ $taxonomy ] ) ? sanitize_text_field( $_GET[ $taxonomy ] ) : '';
  6. $current_status = isset( $_GET['post_status'] ) ? sanitize_text_field( $_GET['post_status'] ) : '';
  7. // 更改为您想要添加的分类
  8. $category_links = array(
  9. 'clothes' => '服装',
  10. 'shoes' => '鞋类',
  11. );
  12. $output = array();
  13. foreach ( $views as $key => $view ) {
  14. $output[ $key ] = $view;
  15. if ( $key === 'publish' ) {
  16. foreach ( $category_links as $slug => $name ) {
  17. $class = ( $slug === $current_term ) ? 'current' : '';
  18. $url = add_query_arg(
  19. array(
  20. $taxonomy => $slug,
  21. 'post_status' => $current_status,
  22. 'post_type' => 'product',
  23. 'paged' => 1,
  24. ),
  25. admin_url( 'edit.php' )
  26. );
  27. $output[ 'category-' . $slug ] = "<a href='$url' class='$class'>$name</a>";
  28. }
  29. }
  30. }
  31. return $output;
  32. }
  33. add_filter( 'views_edit-product', 'add_custom_category_links' );

请注意,我已将分类的名称从英文翻译为中文。如果您需要进一步的帮助,请随时告诉我。

英文:

Here is how you can add these categories - https://prnt.sc/gFuhHx_BKupB

  1. function add_custom_category_links( $views ) {
  2. global $wp_query;
  3. $taxonomy = &#39;product_cat&#39;;
  4. $current_term = isset( $_GET[ $taxonomy ] ) ? sanitize_text_field( $_GET[ $taxonomy ] ) : &#39;&#39;;
  5. $current_status = isset( $_GET[&#39;post_status&#39;] ) ? sanitize_text_field( $_GET[&#39;post_status&#39;] ) : &#39;&#39;;
  6. //Change to categories you want to add
  7. $category_links = array(
  8. &#39;clothes&#39; =&gt; &#39;Clothes&#39;,
  9. &#39;shoes&#39; =&gt; &#39;Shoes&#39;,
  10. );
  11. $output = array();
  12. foreach ( $views as $key =&gt; $view ) {
  13. $output[ $key ] = $view;
  14. if ( $key === &#39;publish&#39; ) {
  15. foreach ( $category_links as $slug =&gt; $name ) {
  16. $class = ( $slug === $current_term ) ? &#39;current&#39; : &#39;&#39;;
  17. $url = add_query_arg(
  18. array(
  19. $taxonomy =&gt; $slug,
  20. &#39;post_status&#39; =&gt; $current_status,
  21. &#39;post_type&#39; =&gt; &#39;product&#39;,
  22. &#39;paged&#39; =&gt; 1,
  23. ),
  24. admin_url( &#39;edit.php&#39; )
  25. );
  26. $output[ &#39;category-&#39; . $slug ] = &quot;&lt;a href=&#39;$url&#39; class=&#39;$class&#39;&gt;$name&lt;/a&gt;&quot;;
  27. }
  28. }
  29. }
  30. return $output;
  31. }
  32. 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:

确定