Adding Structured Data to Magento for Google Merchant (JSON-LD & Microdata)

 

Adding Structured Data to Magento for Google Merchant (JSON-LD & Microdata)

In this article, I will explain in an easy way how to add the correct structured data to Magento for Google Shopping using Microdata or JSON-LD.

What template are you using?

First, we need to know which template you are using.

Login to the Magento Admin panel and go to System > Configuration

Under Configuration click on Design

If you have a Package only.

Than select Package and write down the current package name, for me this is rwd

If you have a Package and Theme selected.

Than select Package and write down the current package name, for me this is default
And write down the name of the theme that you are using. For me this is cenitouch
Magento Find Template Package and ThemeNavigate to the file that needs editing
Open up your favorite FTP program or an IDE with FTP option.

Navigate to the following location and open the file view.phtml

Without Theme

app > design > frontend > rwd > default > template > catalog > product > view.phtml

With Theme

app > design > frontend > default > cenitouch > template > catalog > product > view.phtml

Magento Theme Hierarchy

Before editing always create a backup, for example, copy the file and rename it to view.phtml.org


The below code is for English and US countries, where the decimal separator is a dot.

Add the following code to the bottom of the file view.phtml

JSON LD

<script type="application/ld+json">
{
  "@context": "http://schema.org/",
  "@type": "Product",
  "name": "<?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?>",
    "offers": {
    "@type": "Offer",
    "priceCurrency": "<?php echo $currency_code = Mage::app()->getStore()->getCurrentCurrencyCode(); ?>",
	"image" : "<?php echo $_product->getImageUrl(); ?>",
	"description" : "<?php echo $_helper->productAttribute($_product, nl2br($_product->getShortDescription()), 'short_description') ?>",
	"url": "<?php echo $_product->getProductUrl(); ?>",
	<?php 
	$specialPrice = $_product->getFinalPrice();
	$normalPrice = $_product->getPrice();
	if ($normalPrice != $specialPrice){
	?>
	  "price": "<?php echo preg_replace('/[^0-9.]+/', '',strip_tags(Mage::helper('core')->currency($_product->getFinalPrice()))); ?>",
	 <?php
	}else{
	?>
		"price": "<?php echo preg_replace('/[^0-9.]+/', '',strip_tags(Mage::helper('core')->currency($_product->getPrice()))); ?>",
	<?php
	}
	?>
	"itemCondition" : "http://schema.org/NewCondition",
	<?php
    $microdata_stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getIsInStock();
    if ($microdata_stock >= 1){
      $microdata_stock_msg = 'In Stock';
    }else{
      $microdata_stock_msg = 'Out of Stock';
    }
    ?>
	"availability" : "<?php echo $microdata_stock_msg; ?>"
  }
}
</script>

Microdata

<!-- MICRODATA -->
<div itemscope itemtype="http://schema.org/Product">
  <meta itemprop="name" content="<?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?>" />
  <div itemprop="offers" itemscope="" itemtype="http://schema.org/Offer">
    <meta itemprop="priceCurrency" content="<?php echo $currency_code = Mage::app()->getStore()->getCurrentCurrencyCode(); ?>">
	<?php 
	$specialPrice = $_product->getFinalPrice();
	$normalPrice = $_product->getPrice();
	if ($normalPrice != $specialPrice){
	?>
	  <meta itemprop="price" content="<?php echo preg_replace('/[^0-9.]+/', '',strip_tags(Mage::helper('core')->currency($_product->getFinalPrice()))); ?>">
	 <?php
	}else{
	?>
		<meta itemprop="price" content="<?php echo preg_replace('/[^0-9.]+/', '',strip_tags(Mage::helper('core')->currency($_product->getPrice()))); ?>">
	<?php
	}
	?>
    <?php
    $microdata_stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getIsInStock();
    if ($microdata_stock >= 1){
      $microdata_stock_msg = 'In Stock';
    }else{
      $microdata_stock_msg = 'Out of Stock';
    }
    ?>
	<meta itemprop="image" content="<?php echo $_product->getImageUrl(); ?>">
    <meta itemprop="availability" content="<?php echo $microdata_stock_msg; ?>">
    <meta itemprop="description" content="<?php echo $_helper->productAttribute($_product, nl2br($_product->getShortDescription()), 'short_description') ?>">
    <meta itemprop="url" content="<?php echo $_product->getProductUrl(); ?>">
    <meta itemprop="itemCondition" itemtype="http://schema.org/OfferItemCondition" content="http://schema.org/NewCondition" />
  </div>
</div>
<!-- MICRODATA -->

The code below is for mainland Europe where you use a comma as a decimal separator.

JSON-LD

<script type="application/ld+json">
{
  "@context": "http://schema.org/",
  "@type": "Product",
  "name": "<?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?>",
    "offers": {
    "@type": "Offer",
    "priceCurrency": "<?php echo $currency_code = Mage::app()->getStore()->getCurrentCurrencyCode(); ?>",
	"image" : "<?php echo $_product->getImageUrl(); ?>",
	"description" : "<?php echo $_helper->productAttribute($_product, nl2br($_product->getShortDescription()), 'short_description') ?>",
	"url": "<?php echo $_product->getProductUrl(); ?>",
	<?php 
	$specialPrice = $_product->getFinalPrice();
	$normalPrice = $_product->getPrice();
	if ($normalPrice != $specialPrice){
	?>
	  "price": "<?php echo preg_replace('/[^0-9.]+/', '',str_replace(',','.',str_replace('.','',strip_tags(Mage::helper('core')->currency($_product->getFinalPrice()))))); ?>",
	 <?php
	}else{
	?>
		"price": "<?php echo preg_replace('/[^0-9.]+/', '',str_replace(',','.',str_replace('.','',strip_tags(Mage::helper('core')->currency($_product->getPrice()))))); ?>",
	<?php
	}
	?>
	"itemCondition" : "http://schema.org/NewCondition",
	<?php
    $microdata_stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getIsInStock();
    if ($microdata_stock >= 1){
      $microdata_stock_msg = 'In Stock';
    }else{
      $microdata_stock_msg = 'Out of Stock';
    }
    ?>
	"availability" : "<?php echo $microdata_stock_msg; ?>"
  }
}
</script>

Microdata

<!-- MICRODATA -->
<div itemscope itemtype="http://schema.org/Product">
  <meta itemprop="name" content="<?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?>" />
  <div itemprop="offers" itemscope="" itemtype="http://schema.org/Offer">
    <meta itemprop="priceCurrency" content="<?php echo $currency_code = Mage::app()->getStore()->getCurrentCurrencyCode(); ?>">
	<?php 
	$specialPrice = $_product->getFinalPrice();
	$normalPrice = $_product->getPrice();
	if ($normalPrice != $specialPrice){
	?>
    <meta itemprop="price" content="<?php echo preg_replace('/[^0-9.]+/', '',str_replace(',','.',str_replace('.','',strip_tags(Mage::helper('core')->currency($_product->getFinalPrice()))))); ?>">
	 <?php
	}else{
	?>
    <meta itemprop="price" content="<?php echo preg_replace('/[^0-9.]+/', '',str_replace(',','.',str_replace('.','',strip_tags(Mage::helper('core')->currency($_product->getPrice()))))); ?>">
	<?php
	}
	?>
    <?php
    $microdata_stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getIsInStock();
    if ($microdata_stock >= 1){
      $microdata_stock_msg = 'In Stock';
    }else{
      $microdata_stock_msg = 'Out of Stock';
    }
    ?>
	<meta itemprop="image" content="<?php echo $_product->getImageUrl(); ?>">
    <meta itemprop="availability" content="<?php echo $microdata_stock_msg; ?>">
    <meta itemprop="description" content="<?php echo $_helper->productAttribute($_product, nl2br($_product->getShortDescription()), 'short_description') ?>">
    <meta itemprop="url" content="<?php echo $_product->getProductUrl(); ?>">
    <meta itemprop="itemCondition" itemtype="http://schema.org/OfferItemCondition" content="http://schema.org/NewCondition" />
  </div>
</div>
<!-- MICRODATA -->

Save the file and check if the product landing page has no errors.

Important

If you have existing schema data than you need to ether delete the old schema data or copy elements you need from the code above into your existing schema. If you don’t do this, than Google will see two product listings which will result in errors.

For Example, if I already have the price, priceCurrency and availability, but do not have itemcondition. Than you need to copy the one line itemcondition and paste it inside the offers container. And not create a new schema structure.

First locate your existing schema structure in your products.liquid file, than follow the steps below depending on the type of structure integration.

JSON-LD

Find :

"offers": {
    "@type": "Offer",

And place the below code right after the comma

"itemCondition" : "http://schema.org/NewCondition",

Microdata

Find :

<div itemprop="offers" itemscope="" itemtype="http://schema.org/Offer">

And place the below code inside the div element.

<meta itemprop="itemCondition" itemtype="http://schema.org/OfferItemCondition" content="http://schema.org/NewCondition" />

Checking The Results

You can use Google’s Structured data testing tool, to check if everything is done correctly. The below image shows an example of a correct listing.


Tip :
Copy your landing page link (address bar URL) and paste it in the field for fetching the URL.
Structured Data Testing Tool URL

Google Structured Data Testing Result


Notice :
When you have warnings in your diagnostics tab, remember that it takes up to 30 days before they will disappear, this is because Google does not crawl your landing pages every day but in a span of 30 days.

Need Help?