drnpwmq4536 2017-11-08 14:48
浏览 109
已采纳

默认情况下,使用产品属性值过滤Woocommerce存档页面

I would like products archive pages to display products corresponding to a specific attribute value by default.

Example
Attribute: pa_condition
Terms: New, Used, Closeout

I would like to see only new products when opening the shop.

But there are 2 languages. So I guess, the condition should apply to the attribute id.

How this can be done?

  • 写回答

1条回答 默认 最新

  • doulierong0334 2017-11-08 15:09
    关注

    Updated: You can try this hooked function:

    add_filter( 'woocommerce_product_query_tax_query', 'custom_product_query_tax_query', 10, 2 );
    function custom_product_query_tax_query( $tax_query, $query ) {
        if( is_admin() ) return $tax_query;
    
        // Define HERE the product attribute and the terms
        $taxonomy = 'pa_condition';
        $terms = array( 'New', 'Used', 'Closeout' ); // Term names
    
        // Add your criteria
        $tax_query[] = array(
            'taxonomy' => $taxonomy,
            'field'    => 'name', // Or 'slug' or 'term_id'
            'terms'    => $terms,
        );
        return $tax_query;
    }
    

    Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

    Tested and works.

    But I am not sure about the translations of the terms, so you should be oblige to add them in a second $tax_query[] array in the query for that translated terms…


    Official reference: WP_Query ~ Taxonomy Parameters

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择