首页 文章

WooCommerce REST API:按多个属性查询产品

提问于
浏览
0

根据API:http://woocommerce.github.io/woocommerce-rest-api-docs/#list-all-products,我们可以通过单个属性过滤产品 . 但是通过API搜索多个属性是不可能的?

示例:“我想要 red 衬衫” . 此属性为 color ,属性term为 red . 要完成搜索,查询字符串如下所示: products?category=17&attribute=pa_color&attribute_term=22&

我们只买红衬衫 .

但是对于“我想要 red medium 衬衫”,这里会遇到另一个值为 mediumsize 属性 . 并且根据API,无法将查询字符串中的 colorsize 属性关联起来 . 所以查询 -

products?category=17&attribute=pa_color&attribute_term=22&attribute=pa_size&attribute_term=24&

从商店返回所有产品

有没有解决方法?

2 回答

  • 0

    尝试这样请求:

    products?attribute=pa_color&attribute_term=51,50&per_page=100
    

    它对我有用 . 最新的woocommerce-api wc / v2!

  • 0

    我刚看了一下woocommerce rest api的实现

    plugins/woocommerce/includes/api/class-wc-rest-products-controller.php
    

    文件,但不幸的是他们的代码不支持多个属性 . 但是您可以编写自己的查询来实现目标 .

    $args = array(
    'post_type' => 'product',
    'tax_query' => array(
        array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'pa_color',
                'field'    => 'term_id',
                'terms'    => array( 'red' ),
            ),
            array(
                'taxonomy' => 'pa_size',
                'field'    => 'term_id',
                'terms'    => array( 'Long' ),
            ),
        ),
    ),
    );
    $products = new WP_Query( $args );
    print_r($products);
    

相关问题