WooCommerce Google Product Feed
Adding a Google Product feed in WooCommerce is pretty easy to do if yourself if you don’t want to buy the Google Product Feed Plugin for this from the WooCommerce store.
First you have to create the product feed, so you will need to add the following to your functions.php file, this will then create your product feed.
//Define the product feed php page function products_feed_rss2() { $rss_template = get_template_directory() . '/product-feed.php'; load_template ( $rss_template ); } //Add the product feed RSS add_action('do_feed_products', 'products_feed_rss2', 10, 1);
Line 3 in the above you are setting the php page that will be called when you query your rss feed. Line 4 simply just loads this template so that the WordPress environment is available to it.
The next part on line 8 is the part that adds your feed to WordPress.
Rewrite rules
You should then add some rewrite rules so that you can have the query /feed/products to make it nicer to get to and match the rest of your site.
//Update the Rerewrite rules add_action('init', 'my_add_product_feed'); //function to add the rewrite rules function my_rewrite_product_rules( $wp_rewrite ) { $new_rules = array( 'feed/(.+)' => 'index.php?feed='.$wp_rewrite->preg_index(1) ); $wp_rewrite->rules = $new_rules + $wp_rewrite->rules; } //add the rewrite rule function my_add_product_feed( ) { global $wp_rewrite; add_action('generate_rewrite_rules', 'my_rewrite_product_rules'); $wp_rewrite->flush_rules(); }
From the above line 2 is calling the ‘my_add_product_feed’ procedure to setup the rewrite.
Line 15 is calling the actual function to set-up the rewrite rules. The rewrite rules are setting the part following ?feed=<this part> and rewriting the URL to be /feed/<this part>.
Line 16 is flushing the rules so that they are available.
Once all that is set-up you can then add the actual code that will create your RSS feed. This is very easy as well as there is a nice template for you to copy. Just copy “/wp-includes/feed-rss2.php” into your new php page that you referenced in the above code in your theme directory e.g. “product-feed.php” in the above example.
<?php /** * RSS2 Feed Template for displaying RSS2 Posts feed. * * @package WordPress */ header('Content-Type: ' . feed_content_type('rss-http') . '; charset=' . get_option('blog_charset'), true); $more = 1; echo '<?xml version="1.0" encoding="'.get_option('blog_charset').'"?'.'>'; ?> <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:g="http://base.google.com/ns/1.0" <?php do_action('rss2_ns'); ?> > <channel> <title><?php bloginfo_rss('name'); wp_title_rss(); ?></title> <atom:link href="<?php self_link(); ?>" rel="self" type="application/rss+xml" /> <link><?php bloginfo_rss('url') ?></link> <description>Skin care products for rosacea sufferers</description> <lastBuildDate><?php echo mysql2date('D, d M Y H:i:s +0000', get_lastpostmodified('GMT'), false); ?></lastBuildDate> <language><?php bloginfo_rss( 'language' ); ?></language> <sy:updatePeriod><?php echo apply_filters( 'rss_update_period', 'hourly' ); ?></sy:updatePeriod> <sy:updateFrequency><?php echo apply_filters( 'rss_update_frequency', '1' ); ?></sy:updateFrequency> <?php do_action('rss2_head'); ?> <?php $args = array( 'post_type' => 'product', 'posts_per_page' => 999 ); $loop = new WP_Query( $args ); while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?> <item> <title><?php the_title_rss() ?></title> <link><?php the_permalink_rss() ?></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>in stock</g:availability> <g:google_product_category>Health & Beauty > Personal Care > Cosmetics > Skin Care</g:google_product_category> <?php if (get_option('rss_use_excerpt')) : ?> <description><![CDATA[<?php the_excerpt_rss() ?>]]></description> <?php else : ?> <description><![CDATA[<?php the_excerpt_rss() ?>]]></description> <?php endif; ?> <?php rss_enclosure(); ?> <?php do_action('rss2_item'); ?> </item> <?php endwhile; ?> </channel> </rss>
You can then add the details of the products that you want to display in your RSS feed. You will also have to add the details of google base to the rss header as can be seen on line 20 above. Currently this is listing all products whether they are visible or not and it is also hard coding the google product category so all these parts can be updated but it is a good starter for 10. You have access to all WordPress funcations.
Once all this is there you should be able to access your product XML feed by going to “http://<your URL>/feed/products”.
Update to allow the have dynamic google categories
If you want to manage the Google Product category in the products page then you can change line 47 to the following:
<g:google_product_category><?php echo get_post_meta( get_the_ID(), 'googlecategory', true ); ?></g:google_product_category>
Then in the products if you add a new custom field called googlecategory and enter the associated google category that you want. This will then allow you to have items in the different google categories.
87 Comments
erhan
January 7, 2013hi,
do i have to edit the codes before? or can i copy and paste them 1:1 ?
james
January 7, 2013You would need to edit the description at the top on line 28 and probably the google product category on line 47 but other than that should pretty much work as is.
erhan
January 17, 2013Hi,
google needs
google_product_category
link
image link
brand
gtin
mpn
shipping weight
etc.
can google shopping find all these things automatically?
james
January 17, 2013The google product feed can only find these if you put them in the product feed you create and have the correct XML tags against the element.
erhan
January 18, 2013should i have to put the attributes in line 39? what is if i have different
google_product_category
and
conditions?
james
January 18, 2013Yes you would need to modify the above if you have different google_product_category and conditions.
For google_product_category you can edit line 47
For condition edit line 44
You can get the product_category text by having a look at http://support.google.com/merchants/bin/answer.py?hl=en&answer=160081
erhan
January 19, 2013Hi,
what is if i have different conditions for example some products the are new some are refurbished or some are used. how can i set it in the code?
or what is if i have different google_product_category? some products are for example
“Health & Beauty > Personal Care > Cosmetics > Bath & Body > Bar Soap”
some are
“Health & Beauty > Personal Care > Hair Care > Shampoo & Conditioner”
who can i handle it?
james
January 19, 2013You would just write some php code to output either new, used or refurbished. For the product category you would do exactly the same, just a bit of php to output the appropriate category.
erhan
January 21, 2013what kind of php should i write?
james
January 21, 2013Standard PHP to query the data held in wordpress.
Erhan
January 22, 2013hi,
so when i do it with your code i will have only one google_product_category and one condition? is it correct?
james
January 23, 2013Yes that is correct, this is just for the products that I needed to get listed, it would require slight modification if you wanted more variety in the products.
Wordpres eshop - moje poznatky » blog
February 1, 2013[…] http://www.binaryforest.com/woocommerce-google-product-feed/ […]
erhan
February 11, 2013Hi,
what do you mean with:
Once all this is there you should be able to access your product XML feed by going to “http:///feed/products”.
should i have to upload my product-feed.php file to
/public_html/feed/products
?
james
February 12, 2013You upload your product-feed.php to where you specified in the code:
$rss_template = get_template_directory() . ‘/product-feed.php’;
The first block of code above, so in this example your template directory.
erhan
February 11, 2013and i get an error in line 8
Fatal error: Call to undefined function feed_content_type() in /home/komilieu/public_html/feed/products/product-feed.php on line 8
line 8:
header(‘Content-Type: ‘ . feed_content_type(‘rss-http’) . ‘; charset=’ . get_option(‘blog_charset’), true);
what is wrong here?
erhan
February 11, 2013here the error reports:
[11-Feb-2013 18:48:41] PHP Parse error: syntax error, unexpected T_STRING in /home/komilieu/public_html/feed/products/product-feed.php on line 11
[11-Feb-2013 18:56:55] PHP Fatal error: Call to undefined function feed_content_type() in /home/komilieu/public_html/feed/products/product-feed.php on line 8
james
February 12, 2013Try copying /wp-includes/feed-rss2.php as I mentioned in the above to create your product-feed.php to see if you get any errors then.
Erhan
February 12, 2013Hi,
i have done it before. i dont know what i do wrong. do you have an idea?
james
February 13, 2013Does your normal RSS feeds work? Does http:///feed/?post_type=product
work?
Erhan
February 13, 2013how can i found it out or test it?
james
February 14, 2013Go to your URL and add /feed e.g. http://someurl/feed
Erhan
February 15, 2013I get the index of feed. there is a folder called products.
http://www.komili.eu/feed/
does it works now?
james
February 15, 2013OK, I think I see what your problem is, if you view the source of your page:
http://www.komili.eu/feed/products
You will see that the error is pointing to what I believe is the & sign and the > sign, this needs to be the HTML code, see my example (line 47) above.
james
February 17, 2013Just checked your feed, good to see you have it working now.
Duncan
March 5, 2013Hi, could this be adapted to generate a Product xml file then save to a location on the server for a mobile app to pick up and use?
thanks
Dunk
james
March 15, 2013I can’t see why not you could just schedule a get web page and save it to a file or just update it to write the output direct to file and schedule a regular run of the page to keep the file up to date.
Adrien
March 15, 2013Trying to get your code to work. i have added both the product-feed.php file and the updated to the functions.php file. My normal feed is working, and if i add the rss2 feed file where the product-feed.php file goes the rss2 feed works. If i add your product-feed.php file i receive a blank XML sheet.
Adrien
March 15, 2013I have gone line by line in the product-feed.php and my suspicion is lines 34-38 as when i add that php code the XML goes blank compared to the rss2 file
james
March 16, 2013When you say it goes blank have you had a look at the source of the page to make sure it isn’t just invalid XML? For your product category line 47 are you using HTML codes for the & and > characters?
Tim
March 21, 2013This is great. Thanks James. If anyone can get rounds to filtering by product category I’d be made up.
james
March 21, 2013Hi Tim, what do you mean filtering by product category, if you want to change what you are returning then you can update line 35 (arguments to WP_Query) to specify a more specific query.
I’ve also updated the post to specify how to add a google product category to the product info and use this for the XML output if you have multiple google product categories.
Blokof
June 7, 2013Im stil getting the error: Parse error: syntax error, unexpected T_STRING on line 11..
How can i fix this?
Is this code good?
header(‘Content-Type: ‘ . feed_content_type(‘rss-http’) . ‘; charset=’ . get_option(‘blog_charset’), true);
$more = 1;
?><?php echo '’; ?>
Hope anybody can help me!
The link voor the normal rss feed works okey and i have permalinks on.
james
June 7, 2013It looks like your line 11 is different to the one in the above example,
After the $more = 1; line it is the start of the echo for the xml content:
echo ‘‘; ?>
Blokof
June 10, 2013I changed it to the exact code from above but is still get the error line.. is thing there’s something wrong with the content type?!
Does anybody just have the right file for me?
james
June 10, 2013As suggested in the article above:
Once all that is set-up you can then add the actual code that will create your RSS feed. This is very easy as well as there is a nice template for you to copy. Just copy “/wp-includes/feed-rss2.php” into your new php page that you referenced in the above code in your theme directory e.g. “product-feed.php” in the above example.
Then modify from there, this should work as you said that your normal RSS feed works.
Amy
June 11, 2013James, this is wonderful, thank you for sharing! I’ve uploaded my product feed after doing the above steps successfully and Google can’t find the associated image with each product. There are other warnings, like mpn, brand, and product type, but I’d like the image to show. Can you help? Thanks!
james
June 11, 2013I’m pleased you are finding it useful Amy.
Is it just some of the products or all the products that don’t have images, as looking at your Product XML feed some of your products don’t have associated images?
The other warnings are for details which aren’t always required, e.g. mpn (Manufacturer Part Number) not something all products will have if you want to remove the warning you can add it to the XML with a value of FALSE which would be appropriate for your items.
You can see the details of all the attributes at https://support.google.com/merchants/answer/188494
Amy
June 12, 2013Thanks for the quick response 🙂 All of my products have images, and they all are showing the warning.
Amy
June 12, 2013Ignore my last comment! I just found the place in the merchant account that shows more detail on the warnings, and it is the 4 products that I’m using the default image for. So we’re good!
james
June 12, 2013I’m pleased it is all sorted, good luck with it all.
Blokof
June 18, 2013Now i have no products in my feed, what is wrong?
http://www.purestevia.nl/shop/feed/products/
Please help me!
james
June 18, 2013You have a number of different errors, just do like I say above:
Just copy “/wp-includes/feed-rss2.php” into your new php page that you referenced in the above code in your theme directory e.g. “product-feed.php” in the above example.
To make sure it works then start adding the updates as detailed above.
Some of your errors:
1st line should have the rss version = 2 on a new line
2nd you are closing the rss version =2 part when it should be closed after all the xmlns:xxxx bits below it.
Blokof
June 20, 2013Its now only showing my posts and not my WooCommerce products, how can i edit this to work?
Thnx!!
Jerry
June 23, 2013I keep getting a 404 Response Code when trying to use http:///feed/products as the URL for my data feed. What am I missing?
The URL works if I visit it in my browser.
Thanks for this tutorial btw
james
June 23, 2013Have you tried to save your product feed from your browser? I’ve seen on some web hosts it doesn’t allow you to save it, not really looked into why though. If you can’t save it in your browser this would be why you’re getting the 404 in the data feed.
pixelboy
March 25, 2014In some cases web browsers such as Chrome might display the feed, but in other browsers such as Firefox nothing is returned – in fact a 404 error is generated. This is a problem if you’re submitting your product feed to shopping comparison sites etc. who use crawlers to pull in your feed data. The crawlers will also get the 404 error.
The solution for me was to ensure there is at least 1 published post in your WordPress site. This forces WP to ‘activate’ the RSS feed functionality/permalinks etc. and the 404 problem goes away….hopefully.
Blokof
July 4, 2013Hello,
It still only shows the posts from the blog but not the WooCommerce products!!
Can somebody help me please?
james
July 4, 2013You just have to follow what it say above as it works for everyone else, read it carefully as you must of missed something.
Blokof
July 5, 2013Its working now! Thankyou!
james
July 6, 2013I’m pleased it is now working for you.
Tanya
October 27, 2013Do you think that because all this extra information is necessary for an ecommerce shop, that it should be built in to like say woo commerce so we only have to fill in the appropriate boxes.
I’m no code expert and I’m really struggling here and I cannot afford to pay someone.
There is a plug in you can buy but it doesn’t take variations and every product I have being homewares has a variation.
Shouldn’t there be a standard that ecommerce packages have to have?
james
October 28, 2013Yes I would have thought that the the paid plugin should support all of the features of the product it is designed for. You can add details about the variations by looping though the product_variation post types.
The following has some details regarding the product_variation loop which you may find useful:
http://stackoverflow.com/questions/18728954/woocommerce-product-variations-google-product-feed-and-nested-loops-in-wordpres
Hope that helps.
Tim
October 30, 2013Hi James, thanks for all your work on this. Mine was happily processing away but google have decided to change some requirements and I now need a Manufacturer Part Number (mpn) attribute. I tried to output our SKU field using:-
but it comes out blank on the feed. Any suggestions?
james
November 1, 2013Sorry can’t see what you put, did you try something like
$product->get_sku();
Tim
November 4, 2013Hi James
I put
“”
Tim
November 4, 2013Sorry I’m having a job pasting the code. I’ll try again
Using the syntax from the RSS
Tim
November 4, 2013I basically copied the
g:id statement and replaced the first id for mpn and the echo id with sku
Sander
April 14, 2014ah sorry missed this comment, Thanks a lot!
Tim
October 30, 2013Hi James, thanks for all your work on this. Mine was happily processing away but google have decided to change some requirements and I now need a Manufacturer Part Number (mpn) attribute. I tried to output our SKU field using:-
but it comes out blank on the feed. Any suggestions?
Sander
April 14, 2014Hello James,
Thank you for this great solution! I’d also like to know how to echo the SKU from my products and add it to the feed. That’s the only thing missing for my feed to be added by google 🙂
Cheers,
Sander
Lara
May 26, 2014I don’t know if you still answer this, but I need to add a gtin field and don’t know how? It’s the error on google merchant.
Thanks if you can help.
james
May 27, 2014To add additional fields you would just include them in the while loop in the item XML section starting on line 39.
Gaston
June 3, 2014Hi James,
I have a strange situation, I hope you can help me….
I have added this code to my website, using FTP, and it was working a couple of hours ago, when I went to http://www.computershop-online.nl/feed/products I could see my Product Feed, I added the feed to my Google Merchant Center, and that system told me that it could not download the feed, an error 500 came up, and now a couple of hours later, when I go now to http://www.computershop-online.nl/feed/products I get now a blank page……
Do you have any suggestions to help me?
james
June 3, 2014I would say have a look in your server logs to see if there are any errors. You could also try taking out a lot of the code to generate just a very basic rss entry page to see if that works and then build on it to see where it stops working.
Gaston
June 3, 2014Hi James,
Thankyou I did what you supposed and now is my Product Feed working, I only can’t get it working on Google Shopping, today I found out that Google needs the EAN, MPN and Brand, and thats to difficult for me to find out how that works……
But the feed is working now, so i can look where else I can use that feed.
james
June 4, 2014That is quite easy, all you have to do is add a couple of extra lines in the while have_posts loop to add the additional details in the format as specified on the google page.
Gaston
June 11, 2014Hi James,
Can you please help me again…… I have at this moment a productfeed that is up and running, the productfeed is located at http://www.computershop-online.nl/feed/products build on the code from this website. And now I want to use the product feed for a price comparing website (kelkoo.nl), they ask for a brand field. I have Woocommerce Brands installed on my website. But I searched and searched for hours on Google to find how to add a field in my product feed from Woocommerce Brands, and i cannot find it.
Do you know how to handle this? I also tried to create a custom field in Woocommerce, filled with a brand, and that comes in my product feed, but with no actual brand inside in, whatever i fill in……
I hope that you can help me with this….. I really dont know what to do anymore… It is very frustrating to find a solution for this ….. It drives me crazy ……
james
June 12, 2014I’ve never used Woocommerce Brands but having a quick look on there website I would say you would use something like:
get_brands( $post_id = $id, $sep = ‘, ‘, $before = ”, $after = ” )
Gaston
June 13, 2014Hi James,
And where do I have to place this in the code? And how to use it, I tried this, but it is not clear for me how to use this……
james
June 13, 2014You would put it in the loop along with all the other properties of the item with the tag that they want, something like:
<brand></brand>
Gaston
June 13, 2014Hi James, I tried what you suggested, but it is a no go! It doesn’t work.
I placed the code as shown below in my product-feed.php which is located in /wp-content/themes/virtue/
price ?>
new
in stock
Electronica > Computers and so on……. with above this the channel and so on….
For test purposes I created a just 2 minutes ago a test article in Woocommerce, and after that done i go to my productfeed with my browser Google Chrome to http://www.computershop-online.nl/feed/products I have a XML productfeed, but again with no brand filled in.
Just likes this :
Computer Shop Online
http://www.computershop-online.nl
Computer Shop Online
Fri, 13 Jun 2014 11:35:00 +0000
nl-NL
hourly
1
http://wordpress.org/?v=3.9.1
Test Artikel A
http://www.computershop-online.nl/winkel/behuizingen/test-artikel-a/
new
3039
in stock
Electronica > Computers
Electronica > Computers
as you can see, again NO Brand filled in……
I really dont know what to do further ……
Maybe someone else a suggestion ??
Reikko
June 26, 2014Hi James,
I am not that techy so please bear with my question. Why am I getting these errors. I followed all your steps.
This page contains the following errors:
error on line 24 at column 16: Namespace prefix g on image_link is not defined
error on line 25 at column 17: Namespace prefix g on price is not defined
error on line 26 at column 21: Namespace prefix g on condition is not defined
error on line 27 at column 14: Namespace prefix g on id is not defined
error on line 28 at column 24: Namespace prefix g on availability is not defined
error on line 29 at column 35: Namespace prefix g on google_product_category is not defined
error on line 36 at column 16: Namespace prefix g on image_link is not defined
error on line 37 at column 17: Namespace prefix g on price is not defined
error on line 38 at column 21: Namespace prefix g on condition is not defined
error on line 39 at column 14: Namespace prefix g on id is not defined
error on line 40 at column 24: Namespace prefix g on availability is not defined
error on line 41 at column 35: Namespace prefix g on google_product_category is not defined
error on line 48 at column 16: Namespace prefix g on image_link is not defined
error on line 49 at column 17: Namespace prefix g on price is not defined
error on line 50 at column 21: Namespace prefix g on condition is not defined
error on line 51 at column 14: Namespace prefix g on id is not defined
error on line 52 at column 24: Namespace prefix g on availability is not defined
error on line 53 at column 35: Namespace prefix g on google_product_category is not defined
Please help. 🙁
james
June 26, 2014It sounds like you have missed some of the code as image_link is around line 42
Reikko
June 26, 2014Hi James,
Thank you for responding quickly.
I already fixed that part but it still gives me an error like this
” error on line 1 at column 35: parsing XML declaration: ‘?>’ expected”
http://evlworld.com/feed/products
Did I mess it up?
james
June 26, 2014I would double check that you have copied the code correctly as your RSS feed stops after the generator tag which is produced on line 33 of the script. So it doesn’t appear to be doing anything after that.
Reikko
June 26, 2014Thanks a lot James.
Just to clarify.
Is this the right feed showing??
http://evlworld.com/feed/products/
Can you please verify?
Thanks.
james
June 26, 2014Yes that looks a lot better although you are missing line 20 which defines g in the top of the output. Looks like your comment have an additional – in the closure part for the other errors.
Reikko
June 26, 2014I will check, but I don’t fully understand what you’re telling me. I am so sorry. Hihihih 🙂
Reikko
June 26, 2014You mean by line 20 – http://wordpress.org/?v=3.8.3?
james
June 26, 2014Yes I see that you have fixed that, you now just have a problem with the additional parts you added for shipping_weight and mpn.
try it like:
and
Hıdır
October 7, 2014Hi, first of all, thank you for this feed trick and I need to ask a question about it:
I want to use more than 1 template (product-feed.php, etc.) for feeding. How it can be possible by modifying code?
Thanks again, sincerely.
james
October 8, 2014Without looking or doing any testing I would say you just need to duplicate the first block of code which defines the product feed but with the additional feeds that you are after.
Line 8 is where you define the feed name e.g. do_feed_ followed by the feed name which then points to the function where you define the php page for the feed.
Hope that works for you.
Bobbie
February 18, 2015Hey James. Hi have a rather simple site. it currently has about 1000 products I need a file to upload to google shopping adwords campaign. How much would you charge me to install the code ona make it work on my site. WordPress woo commerce. I dont care if the attributes. Size and color get uploaded just the product base.
Please email and let me know please
james
February 19, 2015Hi, you would be a lot better going with the plugin on the woo commerce site as it is fully supported/tested etc as I’ve not looked at this in quite some time so don’t know about any problems with the later versions of woo commerce and things. The plugin is at http://www.woothemes.com/products/google-product-feed/
Window Decals
March 4, 2015Thank for the replay James, I purchased the one you suggested and have been setting it up. It has some bug fixes mainly on attributes and variations. But I got it worked out. Thank again for the info.
Jeep Decals
September 25, 2018Just to update, been using the plug in you suggested and have had great support from the author. Works like a charm.