I’m trying to make a Google product feed from Woocommerce – the available plugins to do this don’t support product variations, which is a bit of a nuisance.
I’m working on a clothing shop with multiple sizes and colours of item, so it’s required that the Google product feed should list all available size/colour combinations, and as it’s doing stock control for each variation, there is no point listing the Size 10 White as in stock when all we really have left is Size 14 Brown.
So, I reckon what I need to do is create a product feed that runs a loop for each product, then inside that loop, runs a nested loop for each variation? It’s a bit slow, so please suggest a better way if there is one! At least it will only have to run once a month.
Here’s what I have so far:
<?php
// First get the main product details.
$args = array( 'post_type' => 'product', 'posts_per_page' => 999 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product;
// Now do a second nested loop to get the variations.
$args2 = array( 'post_type' => 'product_variation', 'post_parent' =>'$id');
$variationloop = new WP_Query( $args2 );
while ( $variationloop->have_posts() ) : $variationloop->the_post();
// get the parent of each variation so we can use $parent (is this necessary???)
$parent = get_post($post->post_parent);
?>
I feel that really having done the first loop, I should be able to call things from that, but once the second, variation loop is done, I don’t seem to be able to refer to anything within the first, product loop. Hence get_post($post->post_parent)
And then I do the feed. So far this seems to work:
<item>
<title><?php echo $parent->post_title;?></title>
<link>http://mysite.com/shop/<?php echo $parent->post_name;?></link>
<g:image_link><?php echo wp_get_attachment_url( get_post_thumbnail_id() ) ?></g:image_link>
<g:price><?php echo $product->price ?></g:price>
<g:condition>New</g:condition>
<g:id><?php echo $id; ?></g:id>
<g:availability><?php echo $product->is_in_stock() ? get_option('product_in_stock') : get_option('product_out_stock'); ?></g:availability>
<g:brand>My Brandname</g:brand>
<g:product_type>Clothing & Accessories > Clothing > Swimwear</g:product_type>
<g:google_product_category>Clothing & Accessories > Clothing > Swimwear</g:google_product_category>
<g:shipping_weight><?php echo $product->get_weight();?></g:shipping_weight>
<g:mpn><?php echo $product->get_sku(); ?></g:mpn>
<?php if (get_option('rss_use_excerpt')) : ?>
<description><![CDATA[<?php echo $parent->post_excerpt; ?>]]></description>
<?php else : ?>
<description><![CDATA[<?php echo $parent->post_excerpt; ?>]]></description>
<?php endif; ?>
Only this is incomplete – I’m not sure how to get at all the elements that I need. In particular, how do I retrieve the variation size and colour, which are in a different table?
I managed to get this working, so am leaving details in case it’s useful to anyone.
As per brasofilo’s suggestion, I removed the second loop. Then I got the variations like this, checking with meta_compare to see that they were in stock.
Then I got the colours, sizes, photos etc like this:
Note that the exact name of the attributes will depend what you called that attribute when you set up your variations
Then I just echoed out the values:
post_title;?> – Size
http://mywebsite.com/shop/post_name;?>
ID; ?>
post_excerpt); ?>
GBP
I hope that is helpful to someone!
There is a great free plugin for woocommerce product feed which also support product variation. https://wordpress.org/plugins/webappick-product-feed-for-woocommerce/
This premium plugin developed by ELEX supports even variations. Hope this helps those who are still looking for Google Product Feed integration for WooCommerce which supports Product Variations.
https://elextensions.com/plugin/woocommerce-google-product-feed-plugin/