so 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 cant 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 cant see the product attributes from woocommerce anywhere

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

enter image description here

Does anyone know how i access this information after querying the post? specifically i need to extract the colours and sizes.

1 Answers

1
Marek On

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_....