• 549 Views
  • 4 comments
  • Print

WooCommerce based store owners can easily take advantage of Google Merchant Center to promote their merchandise through Google Shopping and increase sales. In my case one of my client reported this tool to be a great one and they were able to increase their online sales by couple of fold.
Google Merchant Center
Store owners can easily submit their product feed in many different ways. There are both online and offline method. However, I personally prefer to submit an XML based product feed from the root directory of the store URL. In other words the URL for the product feed should look like this:

https://www.yourstore.com/products.xml

Such product feed has many additional benefit over offline method. Perhaps the biggest benefit is to be able to generate the updated product list automatically as go on adding or deleting products. Since Google would check the XML file every day(on an specified time), you wouldn’t have to worry about anything. Google will automatically crawl the data for new items and promote them. Today on this post, I will share a simple snippet that would allow store owners to automatically generate the product feed and update them accordingly.

Let’s take a look at the basic Structure of the RSS 2.0 XML File with a single Product Data.

<?xml version="1.0"?>
<rss version="2.0" xmlns:g="https://base.google.com/ns/1.0">
   <channel>
      <title>The name of your data feed</title>
      <link>https://www.example.com</link>
      <description>A description of your content</description>
      <item>
         <title>Product Title</title>
         <link>https://www.example.com/product-page-url/</link>
         <description>Describe your product here.</description>
         <g:image_link>https://www.example.com/image.jpg</g:image_link>
         <g:price>25</g:price>
         <g:condition>new</g:condition>
         <g:id>1a</g:id>
      </item>
   </channel>
</rss>

I would highly recommend you to read the Google Documentation on RSS 2.0 specification for XML format to learn more. You should also read about the Product Data Specification to better understand that possible attributes that you may or should additionally add on your data feed. This is bit complicated as the requirements changes based on the industry or the type of merchandise you are selling. So, I would leave it up to you to figure that out.

Let’s take a look at our magic snippet.

add_action("save_post","google_merchant_products_feed");
function google_merchant_products(){
   // query products
   $allposts = get_posts(array(
      'numberposts' => -1,
      'orderby' => 'post_date',
      'post_type' => array('product'),
      'order' => 'DESC',));
   // generate xml content
   $gmc = '<?xml version="1.0" encoding="UTF-8"?>';
   $gmc .= '<rss version="2.0" xmlns:g="https://base.google.com/ns/1.0">';
   $gmc .= '<channel>';
   $gmc .= '<title>Website Title</title>';
   $gmc .= '<link>https://www.example.com/</link>';
   $gmc .= '<description>A Description of your content</description>';
   foreach($allposts as $post){
      // get thumb url
      $thumb = wp_get_attachment_url(get_post_thumbnail_id($post->ID));
      // sanitize description
      $desc_r = strip_tags($post->post_content); 
      $desc_t = htmlspecialchars(trim($desc_r)); 
      $desc_p = preg_replace("/\r|\n/", "", $desc_t); 
      $desc = strip_tags($desc_p,'<p><a>');
      setup_postdata($post);
      $gmc .= '<item><g:title>'.get_the_title($post->ID).'</g:title>';
      $gmc .= '<g:link>'.get_permalink($post->ID).'</g:link>';
      $gmc .= '<g:description>'.wp_trim_words($desc, 40, '...').'</g:description>';
      $gmc .= '<g:image_link>'.$thumb.'</g:image_link>';
      $gmc .= '<g:price>'.get_post_meta($post->ID,'_price',true).'</g:price>';
      $gmc .= '<g:condition>new</g:condition>';
      $gmc .= '<g:id>'.get_post_meta($post->ID,'_sku',true).'</g:id>';
      $gmc .= '</item>'; 
   }
   $gmc .= '</channel>';
   $gmc .= '</rss>';
   $feed = fopen(ABSPATH.'products.xml','w');
   // write the file
   fwrite($feed, $gmc);
   // close the file
   fclose($feed);
}

This is it! Simply copy this snippet on your current theme’s functions.php page and update it. From now on, as soon as you create or update any Product from WooCommerce, this snippet will write (may require permission) an XML file at very root directory of your WordPress site and list every single product that you have.

Additional Note

Now, please keep in mind that this snippet will create a very basic product feed from your site. Content within this file can get really complex depending on the “Product Type” (simple, variable etc.) you are creating from WooCommerce. For an example, if you are running a retail store that sales various products from different Brands, you may need to provide additional data (Brand Name, GTIN etc.) to Google Merchant Center before you run any advertising campaign. I wrote a post explaining how you can add product brand for your WooCommerce products, you may find that post useful and may want to incorporate the data within the XML file. Also take a look at this post to incorporate GTIN on your product feed.

Additional supported attributes can also be added within this snippet to make your life bit easier. However, I highly recommend you submit the basic feed to the Google Merchant Center first and then go about upgrading this snippet based on what Google Merchant Center requires you to fix errors.

Disadvantages

This is something you should read it very carefully and try to understand before you come to your own solution based on my discussion. On this snippet, I have hooked the RSS Feed generation function with the following hook.

add_action("save_post","google_merchant_products_feed");

This basically means, the XML generating function will be fired when you are saving any product or even updating any product. This is both problematic and helpful. It’s sort of problematic if you are running a site with thousands of products and being added/updated on daily basis. It will definitely slow down your product editing or updating process and you will notice it (I did). So basically if you are pressing that blue colored “Publish” or “Update” button hundreds of times in a day, your XML Feed will be generated that many times. This whole process may slow down your entire site as well. However, if you have very few (10-50 estimated) products on your site or you do not need to update them or their inventory on daily basis you have nothing to worry about. This snippet is helpful because you would have the up to date XML Feed as soon as you update or add any product.

Best Practice

From my personal experience I have found this snippet to be more useful if you hook it up with an schedule event method of WordPress. What it means is, you can schedule to update your XML Feed generating process on a specific interval (i.e. hourly, daily etc.). So, basically instead of generating your XML Feed all the time, you can update it once in a day. I already discussed on this issue on one of my previous post. Following this method will help you big way, specially if you run a large site with thousands of products and variations.

Hope that helps. Thank you.