1

I have been searching product on basis of their attribute by the refference of woocommerce v1 api documentation.following is the sample code that i tried

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

$getProducts = $woocommerce->get('products',$args);
Pranav Patel
  • 111
  • 1
  • 4
  • 16

1 Answers1

0

There are a lot of answers of your question. Here are some examples from them:

From here:

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

And here:

$product->get_attribute( 'your_attr' );

Instead of getting page $args, define global $product and extract from it any attributes you need.

Getting products using attributes( your question answer, got from 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);
Samvel Aleqsanyan
  • 2,812
  • 4
  • 20
  • 28