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

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.

Then select Package and write down the current package name. For me this is rwd

If you have a Package and Theme selected.

Magento Find Template Package and Theme

Then 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

Navigate 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

<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>

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

<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>

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

Important

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

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.

Copy your landing page link (address bar URL) and paste it in the field for fetching the URL.

Google Structured Data Testing Tool

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 30 days.

Need Help?