Woocommerce Rest API产品过滤它的工作原理?

时间:2018-01-09 07:33:49

标签: php wordpress woocommerce-rest-api

我正在尝试将woocommerce rest API与我的应用程序集成。所有默认操作,如获取所有产品,按类别获取产品等都可以正常运行。

有人可以告诉我如何实施产品过滤器。

贝洛是我的代码。

$data = array(
    'status' => 'publish',
    'category' => '51',
    'per_page' => 100,
    'page' => 1,
    'attribute' => "Color",
    'attribute_term' => "Loft Gray"
);

$results = $woocommerce->get('products', $data)

1 个答案:

答案 0 :(得分:3)

你的问题非常开放。您没有任何代码示例可以显示您的工作情况,例如当您说“按类别获取产品等工作完全正常”时。你还需要什么?

我可以向您展示几个例子,但是谁知道它是否会有所帮助。我假设您已经有一个$ woocommerce连接变量工作...

示例1:

$products = array();

$data = array(
    'status' => 'publish',
    'per_page' => 30,
    'orderby' => 'date',
    'order' => 'asc',
    'featured' => 1
);

$products = $woocommerce->get('products', $data);//returns the first 30 featured products that are published, and sorts them by date

示例2:

$results = array();
$data = array(
    'status' => 'publish',
    'category' => '51',
    'per_page' => 100,
    'page' => 1
    //'filter[posts_per_page]' => '-1', //this was removed in v2 api
);

$results = $woocommerce->get('products', $data);//returns 100 published products of product category ID 51 (get this ID from your CMS)
//This can be used for pagination, since the filter functionality is removed

API文档向您展示了您可以访问的所有不同属性:http://woocommerce.github.io/woocommerce-rest-api-docs/?php#list-all-products

我希望这会有所帮助。如果没有,请询​​问具体问题。