Building a xml google product feed using PHP

By | August 1, 2011

As an owner or developer of an Ecommerce site, it’s important to you to get your products seen by as many people as possible. For e-commerce, online marketing is far more cost effective than traditional marketing (print ads etc). Of the many different online marketing techinques, for example organic SEO, pay per click advertising and affiliate advertising – the Google Merchant Center is the way to get your products featuring in search results. As you can see from the screen-shot below, if your product shows up in the results, it will show the title, image and price of the item with a direct link to the product’s page on your site. This has obvious benefits compared to a regular search result.

google-feed01

Google Merchant Center provides 2 methods for adding your products to its search results – a csv or xml feed. I’m going to focus on the xml feed as this gives the developer the greatest flexibility and is more easy to produce programatically, therefore saving effort in the long run! Once you’ve setup your Merchant Account and submitted your feed, you can set a schedule for how often you want Google to check your feed for new products – I generally set it to check once a week, however this can be varied depending on the volume of products on your site.

Ok – first step is to update your .htaccess file so that you can render your php feed as xml. All you need to do is add the following line:

RewriteEngine on
RewriteRule (.*)\.xml(.*) $1.php$2 [nocase]

Next create a new file called product-feed.php (if you’ve added the above line correctly to your .htaccess file, you should later be able to access this as product-feed.xml in a browser). Before you start creating the xml blocks for each product, there’s a few things you need to do first.


<?php
header("content-type: text/xml;"); // Makes sure the file renders as XML

echo '<?xml version="1.0" ?>
<rss version="2.0" xmlns:g="http://base.google.com/ns/1.0" xmlns:c="http://base.google.com/cns/1.0"> // Set up the rss version and required namespaces
<channel> // Set the channel

<title>The Jewellery Boutique</title> // Set the feed title
<link>http://www.thejewelleryboutique.com/</link> // And a feed link
<description>Google Merchant Center</description>'; // And a feed description

/** Product blocks will go here **/

echo '</channel>

</rss>';
?>

Now get your products into an array and loop through them. The function to get the products below is from a custom CMS, however this will work in exactly the same way with any other CMS, just replace the function!

Note from editor: To get this last bit of code to wrap in this blog I had to insert * remove * in a line below. Please remove the additional text if testing with the example below.

// Gets the array of product data, replace this with the correct function for your CMS...
$products = $productClass->getProducts('prd_id,prd_ref,prd_name,prd_long_description,prd_path, * remove *prd_image_1,prd_image_2,prd_image_3,prd_price,prd_sale_price,prd_designer,prd_material');

// Now loop through each product
foreach ($products as $key => $value) {

// Check Stock level and set the required stock msg, again replace this with the correct function for your CMS...
$stockData = $productClass->GetStock($value['prd_id']);
$stockMsg = 'out of stock';

foreach ($stockData as $stockItem)
{
if ($stockItem['stock'] > 0)
{
$stockMsg = 'in stock';
}
}

// Swap your category names for the relevent Google Taxonomy Term detailed in their documentation
if ($value['cat_name'] === 'Rings')
{
$googleCategory = ' > Rings';
}
else if ($value['cat_name'] === 'Necklaces')
{
$googleCategory = ' > Necklaces';
}
else if ($value['cat_name'] === 'Earrings')
{
$googleCategory = ' > Earrings';
}
else if ($value['cat_name'] === 'Bracelets')
{
$googleCategory = ' > Bracelets';
}
else
{
$googleCategory = '';
}

echo "<item>"; // Now begin the item block
echo "<g:id>{$value['prd_ref']}</g:id>"; // Set a unique id for each item (normally your product ref or item number)
echo "<title>".str_replace('&', '&', $value['prd_name'])."</title>"; // Set the title
echo "<link>http://www.thejewelleryboutique.com{$value['prd_path']}</link>"; // Set the link to the product page
echo "<description>".str_replace(array('’', ' ', '&'), array('', '', '&'), strip_tags($value['prd_long_description']))."</description>"; // Give a description of the product
echo "<g:google_product_category>Apparel & Accessories > Jewelry$googleCategory</g:google_product_category>"; // Set the google taxonomy breadcrumb
echo "<g:product_type>Handmade Jewellery > {$value['cat_name']}</g:product_type>"; // Set your own category name
echo "<g:image_link>http://www.thejewelleryboutique.com/images/products/large/{$value['prd_image_1']}</g:image_link>"; // Give the path to 1st image

if (!empty($value['prd_image_2'])) // If you have more than one image for the product, you can set them here
{
echo "<g:additional_image_link>http://www.thejewelleryboutique.com/images/products/large/{$value['prd_image_2']}</g:additional_image_link>";
}
if (!empty($value['prd_image_3']))
{
echo "<g:additional_image_link>http://www.thejewelleryboutique.com/images/products/large/{$value['prd_image_3']}</g:additional_image_link>";
}

echo "<g:condition>new</g:condition>"; // Set the condition (new or used etc)
echo "<g:availability>$stockMsg</g:availability>"; // Set the stock msg, as we defined earlier
echo "<g:price>{$value['prd_price']} GBP</g:price>"; // Set the price & currency

if ($value['prd_sale_price'] > 0) // Set the sale price, if relevent
{
echo "<g:sale_price>{$value['prd_sale_price']} GBP</g:sale_price>";
}

echo "<g:brand>{$value['prd_brand']}</g:brand>"; // Set the brand
echo "<g:color>{$value['prd_color']}</g:color>"; // Set the color
echo "<g:material>{$value['prd_material']}</g:material>"; // Set the material
echo "</item>";
}


Now that all of your xml is built, upload the file to your web server and test it works by going to www.mysite.com/product-feed.xml. If everything’s worked properly, you should see your php file rendered as xml (it’ll look just like a raw rss feed). Add your new feed to the Google Merchant Center and away you go! In a few days your products will be showing up in the search results.

This blog post was written by James Davies, in-house developer at The Jewellery Boutique.