Add Google Structured Data (formerly Rich Snippets) to your product pages

[color=#ff0000]* Updated to hooks & my_changes version. Check post #5 *[/color]



It’s fairly easy actually



This is my case with v3.0.6, should be ok for v3.x.x and also should be ok for v2.x.x with maybe minor changes regarding the template file and what variables are used for pulled data, depending on your cart version.



This MOD includes one additional function.


  1. Open /addons/discussions/controllers/customer/init.post.php



    Add this function at the end of the file:


<br />
function fn_get_review_count($object_id,$object_type)<br />
{<br />
			global $db_tables;<br />
			<br />
			$discussion = fn_get_discussion($object_id, $object_type);<br />
			<br />
			if (empty($discussion)) {<br />
				return false;<br />
			}<br />
			return db_get_field("SELECT COUNT(b.post_id) as val FROM ?:discussion_rating as a LEFT JOIN ?:discussion_posts as b ON a.post_id = b.post_id WHERE a.thread_id = ?i and b.status = 'A'", $discussion['thread_id']);						<br />
}<br />

```<br />
<br />
2. Open up your product template .tpl file i.e:<br />
/skins/YOUR_SKIN/customer/blocks/product_templates/default_template.tpl (may be different in your case)<br />
<br />
Add this code right at the end of the file:<br />
<br />
```php
<br />
{assign var="product_amountN" value=$product.inventory_amount|default:$product.amount}<br />
{assign var="url" value="`$config.current_location`/`$config.current_url`"|fn_url}<br />
{assign var="average_rating" value=$product.product_id|fn_get_average_rating:"P"}<br />
{assign var="rev_count" value=$product.product_id|fn_get_review_count:"P"}<br />
<br />
<div style="display: none;" itemscope itemtype="http://schema.org/Product"><br />
<span itemprop="name">{$product.product|unescape}</span><br />
	 <span itemprop="image">{$config.current_location}{$product.main_pair.detailed.image_path}</span><br />
  <img itemprop="image" src="{$config.current_location}{$product.main_pair.detailed.http_image_path}"/><br />
	 <span itemprop="description"> {$product.full_description}</span><br />
	 <span itemprop="url">{$url}</span><br />
	 <span itemprop="offers" itemscope itemtype="http://schema.org/Offer"><br />
  <span itemprop="price">{$product.price|format_price:$currencies.$primary_currency:''}</span><br />
   <meta itemprop="priceCurrency" content="YOUR_CURRENCY_CODE" /><br />
   {if $product_amountN > 0}<br />
		  <link itemprop="availability" href="http://schema.org/InStock"><br />
   {else}<br />
		  <link itemprop="availability" href="http://schema.org/OutOfStock"><br />
   {/if}<br />
  </span><br />
  <div itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating"><br />
   <span itemprop="ratingValue">{$average_rating}</span><br />
	  <span itemprop="bestRating">5</span><br />
   <span itemprop="reviewCount">{$rev_count}</span><br />
	</div><br />
</div><br />

```<br />
<br />
That's it!<br />
<br />
Don't forget to change "YOUR_CURRENCY_CODE" (without the quotes) to your currency (EUR, USD etc)<br />
<br />
You can check if it works for you from the google test page at: [url="http://www.google.com/webmasters/tools/richsnippets"]http://www.google.co...ls/richsnippets[/url]<br />
<br />
Enjoy...

I just tried it and it appears to work perfectly!



Thank you preudomme

thanks ! i love cs-cart

[color=#282828][font=arial, verdana, tahoma, sans-serif]Thank you preudomme[/font][/color]

Thank you all for trying out.



I posted this mod but then as I don't like core files modifications, I studied a little bit and figured out how to apply this mod with the lovely hooks and by the help of my_changes add-on.



Here is how it is


  1. Create func.php in /addons/my_changes and paste this function in the file


```php

if ( !defined('AREA') ) { die('Access denied'); }
/* FUNCTION REVIEW COUNT -- START */
function fn_my_changes_get_review_count($object_id,$object_type)
{
global $db_tables;

$discussion = fn_get_discussion($object_id, $object_type);

if (empty($discussion)) {
return false;
}
return db_get_field("SELECT COUNT(b.post_id) as val FROM ?:discussion_rating as a LEFT JOIN ?:discussion_posts as b ON a.post_id = b.post_id WHERE a.thread_id = ?i and b.status = 'A'", $discussion['thread_id']);
}
/* FUNCTION - REVIEW COUNT -- END */
?>

```



2. Create /hooks directory (if not exists) in /skins/YOUR_SKIN/customer/addons/my_changes



3. Check the hook name used in your product details .tpl file. For “default_template.tpl” the hook is defined as:

{hook name=“products:view_main_info”}



So our folder structure should be like this /skins/YOUR_SKIN/customer/addons/my_changes/hooks/products



4. Inside this folder create a file named “view_main_info.post.tpl” . We will add the rich snippets code after the “products:view_main_info” hook



5. Copy the following code in view_main_info.post.tpl


```php

{** Rich Snippets - Product Schema - START **}

{HTML microdata}

{assign var=“product_amountN” value=$product.inventory_amount|default:$product.amount}

{assign var=“url” value=“$config.current_location/$config.current_url”|fn_url}

{assign var=“average_rating” value=$product.product_id|fn_get_average_rating:“P”}

{assign var=“rev_count” value=$product.product_id|fn_my_changes_get_review_count:“P”}



{$product.product|unescape}
{$config.current_location}{$product.main_pair.detailed.image_path}

{$product.full_description}
{$url}

{$product.price|format_price:$currencies.$primary_currency:''}

{if $product_amountN > 0}

{else}

{/if}


{$average_rating}
5
{$rev_count}


{** Rich Snippets - Product Schema - END **}
```

That's it. By using the hooks and my_changes technique it's possible to do anything without altering any core files.

Again don't forget to change YOUR_SKIN and currency code "TRY" to your desired values.

Cheers...

wow - thanks so much - just what I was looking for. Followed your instructions exactly, but I'm not seeing the “YOUR_SKIN” to change? Implemented everything anyway - but am not seeing it show up on my product page…thanks for all the effort here.

[quote name='kaelin' timestamp='1364215817' post='158654']

wow - thanks so much - just what I was looking for. Followed your instructions exactly, but I'm not seeing the “YOUR_SKIN” to change? Implemented everything anyway - but am not seeing it show up on my product page…thanks for all the effort here.

[/quote]



“yourskin” is your skin name. if you are on v3 then it will be skins/basic



John

Hook method doesnt seem to be working for me…

Nevermind just had to clear cache. Anyway All my products show out of stock on the snippet. I do not use inventory for my store So would it be safe to remove this line - [color=#666600][size=2]<[/size][/color][color=#000000][size=2]link itemprop[/size][/color][color=#666600][size=2]=[/size][/color][color=#008800][size=2]“availability”[/size][/color][color=#000000][size=2] href[/size][/color][color=#666600][size=2]=[/size][/color][color=#008800][size=2]“OutOfStock - Schema.org Enumeration Member”[/size][/color][color=#666600][size=2]>[/size][/color]

Has anyone verified this works for V2.2.5 ?? Would love to get it working…

Great mod. Does it work for 2.2.4?

[quote name=‘Gizmo’ timestamp=‘1364891938’ post=‘159144’]

Nevermind just had to clear cache. Anyway All my products show out of stock on the snippet. I do not use inventory for my store So would it be safe to remove this line - [color=#666600][size=2]<[/size][/color][color=#000000][size=2]link itemprop[/size][/color][color=#666600][size=2]=[/size][/color][color=#008800][size=2]“availability”[/size][/color][color=#000000][size=2] href[/size][/color][color=#666600][size=2]=[/size][/color][color=#008800][size=2]“OutOfStock - Schema.org Enumeration Member”[/size][/color][color=#666600][size=2]>[/size][/color]

[/quote]



Probably you have already tested it (as it’s been over 1 month :-) )but it should be safe to do so.

Interesting mod.

its working 3.4, thanks preudomme

Hi

have v3.0.6 ultimate

cant understand this

[color=#282828][font=arial, verdana, tahoma, sans-serif]3. Check the hook name used in your product details .tpl file. For “default_template.tpl” the hook is defined as:[/font][/color]

[color=#282828][font=arial, verdana, tahoma, sans-serif]{hook name=“products:view_main_info”}[/font][/color]



Should i find [color=#282828][font=arial, verdana, tahoma, sans-serif]default_template.tpl and insert this [/font][/color][color=#282828][font=arial, verdana, tahoma, sans-serif]{hook name=“products:view_main_info”} ?[/font][/color]

This doesn't actually work with Google. The entire rich snippet markup is contained within a hidden content:






Google refuses to display rich snippets if the snippet data is actually hidden from the page view - they do not care that it is the same data.



Specifically this is what they said:


[quote][font=arial, sans-serif][size=3]Markup Type:Product[/size][/font]

[font=arial, sans-serif][size=3]Hidden content:

Product - Schema.org Type”]http://schema.org/Product[/url][font=arial, sans-serif][size=3]”>[/size][/font]

[font=arial, sans-serif][size=3]This div which has the markup, is explicitly hidden using style=“display: none;”. The markup tags should be placed in the actual html tags surrounding the actual visible content. Please refer to the documentation at [/size][/font][url=“Product - Schema.org Type”]http://schema.org/Product[/url][font=arial, sans-serif][size=3].[/size][/font][/quote]



…I do not see how this posted method can be made to comply as it relies entirely on generating additional dynamic content specifically for markup, then hiding it from the page. Anyone have any ideas?



V.

As far as I know Google can ban permanently to display the snippets of such store. Don't think it's a good idea, especially as such modification doesn't cost so expensive now.

If this is the case, then it will be better to apply this as a “core mod” rather than a my_changes thing. What I don't understand is that the rich snippets test tool of google shows the preview with no errors or warnings like “div is hidden, please use the tags in the actual visible html tags”. Anyway, those who are not happy with core modifications can of course go and buy an add-on.



Cheers

I added the Rich Snippets data to a view_main_info.pre.tpl so it's appended prior to the product data. I placed the content in META tags as in this example:




The DIV tag is closed in the view_main_info.post template. But all the content tags are META tags, in the body of the HTML.

So far Google has indexed my pages and is including the Price, Stock and Aggregated Rating in my site's search results for products.
I made a change to the way I did this, and included Open Graph formatted content tags in the header of the store (for Facebook sharing correct product image and URL), but after re-indexing by Google, none of the Rich Snippet data was displayed in the Google search results. I removed the Open Graph tags from the header and after another Google crawl the Rich Snippets data is back in the search results.

I hope the use of meta tags doesn't jeopardize my relationship with Google.
It's really rather difficult to use an "in line method" to get the Aggregated Review data included in with the Discussions addon, and still be included in with the "schema.org/Product" div tag - so it is read as a sub-item under the "schema.org/Product" data where it should be.
If anyone has any experience opening a DIV tag in the product data, and closing it after the Discussion addon, or at least wrapping it around the Product name/price/stock and the Description, Reviews and Features in the product tabs, I'd be glad to hear it.

It's working on the google testing tool, but it's more than one month and google refuse to display rich snippets.

It's working for somebody?