产品列表上的 WooCommerce REST API 属性过滤器不起作用

WooCommerce REST API attribute filter on Product list not working

我正在使用最新的 API v2。虽然 min_pricecategory 等其他过滤器正在工作,但 attributeattribute_terms 查询参数不会过滤结果集。

我有一个属性 Color,我正在从 node.js 客户端调用 API 作为 :

var WooCommerceAPI = require('woocommerce-api');
var WooCommerce = new WooCommerceAPI({
  url: 'http://localhost/index.php',
  consumerKey: '***',
  consumerSecret: '***',
  wpAPI: true,
  version: 'wc/v2'
});

WooCommerce.getAsync('products?attribute=pa_color').then(function(result){

        var data = JSON.parse(result.toJSON().body);
        console.log(data);
    });

我怎样才能真正按属性过滤?尝试了其他组合,例如attribute=Colorattribute=color。仍然没有过滤结果。

查看最新的 WooCommerce REST API 文档:http://woocommerce.github.io/woocommerce-rest-api-docs/#list-all-products

WooCommerce 似乎有一段糟糕的 REST API 历史。他们有 API V1、V2 和 V3,然后更改为 WP REST API 并创建了 V1 和 V2。当然,在 Google 上搜索正确的方法非常糟糕。

这应该是正确的做法:

https://example.com/wp-json/wc/v2/products/?attribute=pa_color&attribute_term=15

https://example.com/wp-json/wc/v2/products/?attribute=pa_color&attribute_term=green

注意: 将属性与 attribute_term 一起使用很重要。

我猜以前的方法是这样的:

products?filter[meta_key]=pa_color&filter[meta_value]=green

或者像这样:

https://example.com/wp-json/wc/v2/products?attribute[pa_color]=green

但似乎 这在 WooCommerce WordPress API V2.

中不再有效

我找到了解决办法。正确做法:

products?attribute=pa_color&attribute_term=50

您必须将属性术语的名称替换为属性术语的 ID。为我工作。

P.S。我使用的是 wc/v2.

的最新版本