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
Opening the correct file
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
Adding the code
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>
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>
To add microdate to OpenCart 3x click here
Now save your file and upload. Check a product page to ensure nothing went wrong.
If you have existing schema data then you need to delete the old schema data. If you don’t do this then Google will see two product listings which will result 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.