【问题标题】:Filter Woocommerce archive pages with product attribute values by default默认情况下使用产品属性值过滤 Woocommerce 存档页面
【发布时间】:2017-11-08 14:48:10
【问题描述】:

我希望产品存档页面默认显示与特定属性值对应的产品。

示例
属性:pa_condition
术语:新的、二手的、收尾的

我希望在开店时只看到新产品。

但是有两种语言。所以我想,条件应该适用于属性id。

如何做到这一点?

【问题讨论】:

  • 你试过什么,你的代码是什么?根据您所写的内容,尚不清楚问题可能出在哪里。

标签: php wordpress woocommerce archive taxonomy


【解决方案1】:

更新:你可以试试这个钩子函数:

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

代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。

经过测试并且有效。

但我不确定这些术语的翻译,因此您必须将它们添加到第二个 $tax_query[] 数组中,以查询该翻译的术语……


官方参考:WP_Query ~ Taxonomy Parameters

【讨论】:

  • 它返回“没有符合所选标准的产品”。我刚刚删除了“在此处定义产品属性和条款”。我不确定是否必须保留数组中的所有术语或只是“新”。我不确定是否必须替换“字段”中的“名称”=>
  • 我保留 'field' => 'names',一模一样?还是应该用 New 替换名称?
  • windspirit.gbwebmedia.com/onlinestore/windsurf/windsurf-boards : 没有找到符合您选择的产品。
  • 我在您的代码中所做的更改: $terms = array( 'new', 'used', 'distributors' ); // 术语名称和 'field' => 'new',
  • 对不起@Gaël ......我已经测试过了,女仆做了一些小改动......现在这正在按预期工作。尝试一下。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-02-12
  • 1970-01-01
  • 2018-09-08
  • 2018-09-27
  • 1970-01-01
  • 1970-01-01
  • 2020-08-20
相关资源
最近更新 更多