如何通过api为woocommrce搜索产品的属性?

时间:2018-01-05 12:18:45

标签: php wordpress woocommerce woocommerce-rest-api

我一直在根据他们的属性搜索产品 woocommerce v1 api documentation.following是我试过的示例代码

$args = array(
                "page" => "1",
                "status" => "publish",
                "attribute" => Array
                    (
                        "name" => "Color",
                        "options" => Array("Loft Gray")
                    )
            );

$getProducts = $woocommerce->get('products',$args);

1 个答案:

答案 0 :(得分:0)

你的问题有很多答案。以下是他们的一些例子:

From here

$brand_terms = get_the_terms( $post, 'pa_liquor-brands' );

And here

$product->get_attribute( 'your_attr' );

不是获取页面$ args,而是定义global $product并从中提取您需要的任何属性。

使用products获取attributes(您的问题回答,来自here):

// The query
$products = new WP_Query( array(
   'post_type'      => array('product'),
   'post_status'    => 'publish',
   'posts_per_page' => -1,
   'meta_query'     => array( array(
      'key' => '_visibility',
      'value' => array('catalog', 'visible'),
      'compare' => 'IN',
  ) ),
  'tax_query'      => array( array(
      'taxonomy'        => 'pa_color',
      'field'           => 'slug',
      'terms'           =>  array('blue', 'red', 'green'),
      'operator'        => 'IN',
   ) )
) );

// The Loop
if ( $products->have_posts() ): while ( $products->have_posts() ):
    $products->the_post();
    $product_ids[] = $products->post->ID;
endwhile;
    wp_reset_postdata();
endif;

// TEST: Output the Products IDs
print_r($product_ids);