In this article we will cover in a very simple way how to add the correct structured data for Opencart 1.5x and 2.x using JSON-LD and Microdata
First we will need to locate the correct template file to add the structured data. Open up your FTP or IDE and source the file in the following location.
When you are in the theme folder please select your theme, in this example my theme is uber.
catalog > view > theme > uber > template > product > product.tpl
Before editing always make a copy of the original file for backup purposes. I simple copy it to product.php
OpenCart 1.5x
Once you have opened up product.tpl, the first two lines you should see the following code
<?php echo $header; ?><?php echo $column_left; ?><?php echo $column_right; ?> <div id="content"><?php echo $content_top; ?>
Paste the below code just under the id element content
<script type="application/ld+json"> { "@context": "http://schema.org/", "@type": "Product", "name": "<?php echo $heading_title; ?>", "offers": { "@type": "Offer", "priceCurrency": "<?php echo $this->currency->getCode();?>", "price": "<?php if (!$special) { echo preg_replace('/[^0-9.]+/','',$price);}else{echo preg_replace('/[^0-9.]+/','',$special);} ?>", "itemCondition" : "http://schema.org/NewCondition", "availability" : "<?php if ($stock >= 1){echo 'In Stock';}else{echo $stock;} ?>" } } </script>
<div itemscope itemtype="http://schema.org/Product"> <meta itemprop="name" content="<?php echo $heading_title; ?>" /> <div itemprop="offers" itemscope="" itemtype="http://schema.org/Offer"> <meta itemprop="priceCurrency" content="<?php echo $this->currency->getCode();?>"> <meta itemprop="price" content="<?php if (!$special) { echo preg_replace('/[^0-9.]+/','',$price);}else{echo preg_replace('/[^0-9.]+/','',$special);} ?>"> <meta itemprop="availability" content="<?php if ($stock >= 1){echo 'In Stock';}else{echo $stock;} ?>"> <meta itemprop="itemCondition" itemtype="http://schema.org/OfferItemCondition" content="http://schema.org/NewCondition" /> </div> </div>
OpenCart 2.x
The first line you should see
<?php echo $header; ?>
Paste below the header variable paste the below code
<?php foreach ($_SESSION as $key => $value){$currency = $value['currency'];}?> <script type="application/ld+json"> { "@context": "http://schema.org/", "@type": "Product", "name": "<?php echo $heading_title; ?>", "offers": { "@type": "Offer", "priceCurrency": "<?php echo $currency; ?>", "price": "<?php if (!$special) { echo preg_replace('/[^0-9.]+/','',$price);}else{echo preg_replace('/[^0-9.]+/','',$special);} ?>", "itemCondition" : "http://schema.org/NewCondition", "availability" : "<?php if ($stock >= 1){echo 'In Stock';}else{echo $stock;} ?>" } } </script>
<div itemscope itemtype="http://schema.org/Product"> <meta itemprop="name" content="<?php echo $heading_title; ?>" /> <div itemprop="offers" itemscope="" itemtype="http://schema.org/Offer"> <?php foreach ($_SESSION as $key => $value){$currency = $value['currency'];}?> <meta itemprop="priceCurrency" content="<?php echo $currency; ?>"> <meta itemprop="price" content="<?php if (!$special) { echo preg_replace('/[^0-9.]+/','',$price);}else{echo preg_replace('/[^0-9.]+/','',$special);} ?>"> <meta itemprop="availability" content="<?php if ($stock >= 1){echo 'In Stock';}else{echo $stock;} ?>"> <meta itemprop="itemCondition" itemtype="http://schema.org/OfferItemCondition" content="http://schema.org/NewCondition" /> </div> </div>
To add microdate to OpenCart 3x click here
Now save your file and upload. Check a product page to ensure nothing went wrong.
[focus]
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 offer container.
<meta itemprop="itemCondition" itemtype="http://schema.org/OfferItemCondition" content="http://schema.org/NewCondition" />
[/focus]
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.