Skip to main content

Implement Product Schema.org Markup into Magento

In this tutorial we’ll be looking at how we can add all the main features of Product Schema into your Magento template files – editing the files and then testing it through Google’s own structured data testing tool.

As mentioned in my Magento SEO book, we add product schema into our Magento websites in order for search engines such as Google to be able to identify the main purpose of our page – in this case the main attributes of our product.

The main benefits of adding product schema markup to our pages is for search engines to be able to read them more easily and present ‘rich snippets’ in their own results pages based on the information they find.

These ‘rich snippets’ typically display our product price or price-range and any aggregated reviews that our product may have accumulated. Although not a ranking factor in itself they do present the user with a ‘juicier’ search result – giving your listing a bit of an edge over the competition (those not displayed with rich snippets).

Without rich snippets your listing in the results pages of Google may look a little ‘standard’.

Standard Listing
In this tutorial we’re going to implement the following schema.org on our Magento templates:

Product Schema
Aggregate Rating Schema
Offer Schema
Implementing Product Schema
First of all we must edit our product view.phtml to wrap our dynamic product pages in our new schema.org markup. Open up your [your_theme]/template/catalog/product/view.phtml and edit the following lines:

<div class="product-view">

// to

<div class="product-view" itemscope itemtype="http://schema.org/Product">

// and

<h1><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></h1>

// to

<h1 itemprop="name"><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></h1>

This will wrap our view.phtml inside the “Product” item type. We can then set attributes such as the product name (in this case using the <h1> tag. Now we need to add markup to our product description and our product image.

Still within view.phtml (depending on your theme) find the line that calls our product description (short description):


<div class="std"><?php echo $_helper->productAttribute($_product, nl2br($_product->getShortDescription()), 'short_description') ?></div>

// change to

<div class="std" itemprop="description"><?php echo $_helper->productAttribute($_product, nl2br($_product->getShortDescription()), 'short_description') ?></div>
To add our image attribute to the markup we should venture inside [your_theme]/template/catalog/product/view/media.phtml:


$_img = '<img ....etc

// change to

$_img = '<img itemprop="image" ....etc
Next we need to add our Aggregate Rating Schema.

Implementing AggregateRating Schema
The AggregateRating schema will only show up if there are reviews for our product and also whether our theme supports showing these reviews. Typically within [your_theme]/template/review/helper/summary.phtml we would change the following lines:


<div class="ratings">

// change to

<div class="ratings" itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">
Next, we need to add a way to tell search engines what our best and worst ratings are, as well as what our average rating is for our product and how many reviews we have had in total. To do this we’ll simply add a few hidden fields – it’s always better to wrap visible items in our markup but it depends on what you are displaying on your store. For now we’ll just add a few hidden fields:


// just underneath <div class="ratings" add the following
<span class="no-display" itemprop="reviewCount"><?php echo $this->getReviewsCount() ?> reviews</span>

// then just within <?php if ($this->getRatingSummary()):?> add the following

<span class="no-display" itemprop="worstRating">0</span>
<span class="no-display" itemprop="bestRating">5</span>
<span class="no-display" itemprop="ratingValue"><?php echo round($this->getRatingSummary()/20,1); ?></span>
The above code will set the review count as well as work out the /5 rating average for our product.

Once we’ve added this we should start to see some change on our structured data testing tool:

Reviews and Ratings
Implementing Offer Schema
The last stage is to add in our Offer schema. To do this we would usually go into a couple of files and edit where we bring out out “availability” and “price”. Unfortunately the price.phtml is quite complex – so for simplicities sake we can simply add a hidden field to our [your_theme]/template/catalog/product/view.phtml:

just underneath where we set our <h1 itemprop="name" add the following
<?php if($_product->isAvailable()): ?>
<div class="no-display" itemprop="offers" itemscope itemtype="http://schema.org/Offer">
     <span itemprop="price"><?php echo Mage::helper('core')->currency($_product->getFinalPrice()); ?></span>
     <link itemprop="availability" href="http://schema.org/InStock" />
</div>
<?php endif; ?>
This will check first of all to see if the product is available (in stock) and if so will display our products’ price and availability.

If all of the above has been implemented successfully we should be able to see all 3 schema.org markup’s in action.

Reviews and Prices

Comments

  1. Thats great information. Before this I was familiar with Wordpress schema markup, but after reading this I will definitely implement this in my Magento eCommerce website. Again thanks for this amazing blog. As I'm Magento eCommerce developer and I would like to read the articles related to Magento.

    ReplyDelete
  2. It can be done quickly using the magento extensions
    Magento 1
    Magento 2

    ReplyDelete
  3. This blog is really valuable and informative. Thanks for sharing it. Really enjoyed reading this article and learn some new things like
    ecommerce app source code.

    ReplyDelete
  4. Magento is now a lot easier to use and customize as per your will. The availability of drag and drop feature in Magento themes has changed the dynamics of how Magento was viewed some years ago.

    ReplyDelete

Post a Comment

Popular posts from this blog

magento 1 extension converted in to magento 2 process

Simple Install =================================== Download as a zip below link. unzip and upload it to your public_html or other rootdoc directory. https://github.com/magento/code-migration. login to your server via shell switch to the code-migration-develop directory by running following command   cd code-migration-develop Run command following command in root directory of this toolkit composer instal Before running the migration, the following directories need to be prepared: ===================================== * `<src>` - Directory that contains custom Magento 1.x code that is intended to be migrated. The code must follow the Magento 1.x directory structure.   Magento1.x core files must not be included. * `<dst>` - Empty directory for the toolkit to put the generated Magento 2 code to * `<m1>` - Directory that contains: * Vanilla Magento 1.x codebase, and * Custom Magento 1.x code same as...

Magento 2 product collection Filtering multi-select attribute values

  If you have multi-select attribute of product like below If you want filter value for this option Use below syntax to get product data: ->addAttributeToFilter('store_model', array('finset' => $params['store_model'])) finset key is used for multiselect attribute filter. $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $products = $objectManager->get('Magento\Catalog\Model\Product')         ->getCollection()         ->addAttributeToSelect('*')         ->addAttributeToSelect('store_brand')         ->addAttributeToSelect('store_model')         ->addAttributeToSelect('store_year')         ->addAttributeToFilter('store_brand', array('finset' => $params['store_brand']))         ->addAttributeToFilter('store_model', array('finset' => $params['store_model']))         ->ad...

Magento Debug HEADERS ALREADY SENT error

When you  receiving the following error in  system.log file: app\code\core\Mage\Core\Controller\Response\Http.php:44  [1] \www.pawan.com\lib\Zend\Controller\Response\Abstract.php:727  [2]  \www.pawan.com\app\code\core\Mage\Core\Controller\Response\Http.php:75  [3]  \www.pawan.com\app\code\core\Mage\Core\Controller\Varien\Front.php:188  [4]  \www.pawan.com\app\code\core\Mage\Core\Model\App.php:304  [5]  \www.pawan.com\app\Mage.php:596  [6]  \www.pawan.com\index.php:81 if you have no idea what file is causing this. Here is the steps to debug. That error is thrown from Mage_Core_Controller_Response_Http -> sendHeaders(). This function calls the super class function that actually does the check to see whether or not headers have already been sent, Zend_Controller_Response_Abstract -> canSendHeaders(). The Zend_Controller_Response_Abstract class handles, among other things, sending response header...