0
votes

I am querying the posts as shown below:

query_posts('post_type=Product&showposts=-1');

This returns all post of type product. But I have custom attributes on my products that have been created in wooCommerce but I can't seem find this info anywhere.

I tried using:

$thePost = get_post_custom()

I also tried:

$thePost = get_post_meta(get_the_ID)

when I print_r these I get a lot of information but I can't see the product attributes from woocommerce anywhere

the image below shows how this information is set on the product.

enter image description here

How do I access this information after querying the post? specifically I need to extract the colours and sizes.

1

1 Answers

1
votes

Product variations are saved as another child posts (custom post type product_variation). Following code is untested, but you should get the point.

query_posts('post_type=Product&showposts=-1');
while( have_posts() ){
  the_post();
  $product_id = get_the_ID();  

  $variations = get_posts( array('post_type' => 'product_variation', 'post_parent' => $product_id, 'posts_per_page' => -1 ) );

  foreach( $variations as $var){
    $var_customs = get_post_customs( $var->ID );
    // now you can inspect "meta" fields
  }
}

So you have Product with ID=7 ->post_type=product, its variations are post_type=product_variation&post_parent=7. And sizes and coulours are saved as meta values of these variations. Meta keys start with attribute_pa_....