Created on 24 July, 2015 | Updated on 29 April, 2021
Add Structured Data to Opencart 1.5x & 2x for Google Merchant (JSON-LD & Microdata)

Add Structured Data to Opencart 1.5x & 2x for Google Merchant (JSON-LD)

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.

59 thoughts on “Add Structured Data to Opencart 1.5x & 2x for Google Merchant (JSON-LD)”

    1. Emmanuel Flossie

      Nothing, you only need to install one or the other. They are essentially the same only that JSON-LD is the newer technology. Hope this helps.

  1. I just updated suggested code into my website, but whenever i do a test in Structured Data Testing Tool it return me an Error “The URL timed out. Make sure it is responding in a timely fashion to requests from Google.”

    1. 1) Check that the product landing pages are still working after you added the code, if not then you have done something wrong2) If Google can not crawl your pages, then check your server log for issues and resolve what is causing the issue.Hope this helps.

  2. HI! Added the updated coding – no issues with Google-testing now :)Re price – resolved the issue by changing the decimal symbol to ‘dot’ in catalog/language/mylanguage/mylanguage.phpCan we ‘strech’ a bit by adding image? Also shall Google show this structured data as rich snippet is search results?

  3. Hi again and thanks for quick reply!!! Indeed opencart is 2.1.0.1, sorry. Applied the fix and product pages are OK now. Checking with Google’s structured data testing tool I have mistake for the field ‘name’ – Google requires value for this field.Another minor issue is related with decimal point (I have decimal ‘comma’ and the price was showing 1490 instead of 14,90) – changed a bit the coding (“price” section replaced the ‘dot’ with ‘comma’) and now shows 14,90, but Google warns about this. Obviously need to do it the other way around – to make the site working with ‘dot’, but not with ‘comma’ or it is something related with regional settings.Anyway, I will be thankful for your advice about the field ‘name’.

  4. Hi Emmanuel,I tried it, but I got followin on a product page Warning: Illegal string offset ‘currency’ in /home/podazzp8/public_html/catalog/view/theme/default/template/product/product.tpl on line 2Warning: Illegal string offset ‘currency’ in /home/podazzp8/public_html/catalog/view/theme/default/template/product/product.tpl on line 2Notice: Undefined index: currency in /home/podazzp8/public_html/catalog/view/theme/default/template/product/product.tpl on line 2Warning: Illegal string offset ‘currency’ in /home/podazzp8/public_html/catalog/view/theme/default/template/product/product.tpl on line 2Any advice?Kind regards,Georgi

    1. Emmanuel Flossie

      Remove the first line if opencart 2, that loops for checking the currency, than where you see priceCurrency, change the value between the double quotes to your country currency code, for example : USD or GBPHope it helps.

    1. Emmanuel Flossie

      I guess you meant the price, not the currency? I can’t see any issues with the currency. For the price replace the php code with the following. This is not tested, but you need to remove any foreign characters and just keep the numeric value.

    1. Currently Google Shopping does not require reviews to be added as microdata, hence the reason there is no mention of this. I recommend hiring a web developer for this or ask info on OpenCart forum. I wish you the best of luck.

  5. Hi, I tried this code in my opencart 2.0.3.1 product.tpl file and it messed it up, nothing would load after the menu, the errors in the log are below

    2017-07-13 13:28:56 – PHP Notice: Undefined variable: product in /home/forsweetsake/public_html/vqmod/vqcache/vq2-system_modification_catalog_view_theme_shovelhead_template_product_product.tpl on line 674
    2017-07-13 13:28:56 – PHP Notice: Undefined variable: product in /home/forsweetsake/public_html/vqmod/vqcache/vq2-system_modification_catalog_view_theme_shovelhead_template_product_product.tpl on line 675
    2017-07-13 13:28:56 – PHP Notice: Undefined variable: reviewss in /home/forsweetsake/public_html/vqmod/vqcache/vq2-system_modification_catalog_view_theme_shovelhead_template_product_product.tpl on line 678
    2017-07-13 13:28:56 – PHP Warning: Invalid argument supplied for foreach() in /home/forsweetsake/public_html/vqmod/vqcache/vq2-system_modification_catalog_view_theme_shovelhead_template_product_product.tpl on line 678
    2017-07-13 13:28:56 – PHP Notice: Undefined variable: product in /home/forsweetsake/public_html/vqmod/vqcache/vq2-system_modification_catalog_view_theme_shovelhead_template_product_product.tpl on line 762
    2017-07-13 13:28:56 – PHP Notice: Undefined variable: product in /home/forsweetsake/public_html/vqmod/vqcache/vq2-system_modification_catalog_view_theme_shovelhead_template_product_product.tpl on line 763
    2017-07-13 13:28:56 – PHP Notice: Undefined variable: reviewss in /home/forsweetsake/public_html/vqmod/vqcache/vq2-system_modification_catalog_view_theme_shovelhead_template_product_product.tpl on line 766
    2017-07-13 13:28:56 – PHP Warning: Invalid argument supplied for foreach() in /home/forsweetsake/public_html/vqmod/vqcache/vq2-system_modification_catalog_view_theme_shovelhead_template_product_product.tpl on line 766
    2017-07-13 13:28:56 – PHP Notice: Undefined variable: product in /home/forsweetsake/public_html/vqmod/vqcache/vq2-system_modification_catalog_view_theme_shovelhead_template_product_product.tpl on line 863
    2017-07-13 13:28:56 – PHP Notice: Undefined variable: product in /home/forsweetsake/public_html/vqmod/vqcache/vq2-system_modification_catalog_view_theme_shovelhead_template_product_product.tpl on line 864
    2017-07-13 13:28:56 – PHP Notice: Undefined variable: reviewss in /home/forsweetsake/public_html/vqmod/vqcache/vq2-system_modification_catalog_view_theme_shovelhead_template_product_product.tpl on line 867
    2017-07-13 13:28:56 – PHP Warning: Invalid argument supplied for foreach() in /home/forsweetsake/public_html/vqmod/vqcache/vq2-system_modification_catalog_view_theme_shovelhead_template_product_product.tpl on line 867
    2017-07-13 13:28:56 – PHP Notice: Undefined variable: product in /home/forsweetsake/public_html/vqmod/vqcache/vq2-system_modification_catalog_view_theme_shovelhead_template_product_product.tpl on line 954
    2017-07-13 13:28:56 – PHP Notice: Undefined variable: product in /home/forsweetsake/public_html/vqmod/vqcache/vq2-system_modification_catalog_view_theme_shovelhead_template_product_product.tpl on line 955
    2017-07-13 13:28:56 – PHP Notice: Undefined variable: reviewss in /home/forsweetsake/public_html/vqmod/vqcache/vq2-system_modification_catalog_view_theme_shovelhead_template_product_product.tpl on line 958
    2017-07-13 13:28:56 – PHP Warning: Invalid argument supplied for foreach() in /home/forsweetsake/public_html/vqmod/vqcache/vq2-system_modification_catalog_view_theme_shovelhead_template_product_product.tpl on line 958
    2017-07-13 13:28:56 – PHP Notice: Undefined variable: product in /home/forsweetsake/public_html/vqmod/vqcache/vq2-system_modification_catalog_view_theme_shovelhead_template_product_product.tpl on line 1040
    2017-07-13 13:28:56 – PHP Notice: Undefined variable: product in /home/forsweetsake/public_html/vqmod/vqcache/vq2-system_modification_catalog_view_theme_shovelhead_template_product_product.tpl on line 1041
    2017-07-13 13:28:56 – PHP Notice: Undefined variable: reviewss in /home/forsweetsake/public_html/vqmod/vqcache/vq2-system_modification_catalog_view_theme_shovelhead_template_product_product.tpl on line 1044
    2017-07-13 13:28:56 – PHP Warning: Invalid argument supplied for foreach() in /home/forsweetsake/public_html/vqmod/vqcache/vq2-system_modification_catalog_view_theme_shovelhead_template_product_product.tpl on line 1044

          1. I have done a test as well and it has the following errors/warnings

            quantity
            In Stock (The property quantity is not recognized by Google for an object of type Product.) – think this is because rather than having stock figures displayed on the product page, I got text instead saying In stock or out of stock

            price
            £0.45 (The property £0.45 is not a valid price specification. Learn more about http://schema.org/price.)
            warningpriceCurrency
            The priceCurrency field is recommended. Please provide a value if available.

            JSON-LD
            Syntax error: value, object or array expected.

            Can you help please?

  6. Hi , I am getting this error ” Insufficient match of microdata price information” in my GMC Diagnostics, Our website is built on OC 2.0.3,1 and default currency is INR, but the feeds are showing the prices in USD, what could be the problem please ?

    1. When you are targeting USA with a dedicated data feed, than both the data feed and landing page must be USD. You can not have INR prices when targeting USA. To resolve this make sure the landing page is USD. Hope it helps

        1. Than you must make sure the data feed is also in INR. Check with your plugin tool or management system that you are using. As Google requires both landing page and data feed to have the same language and currency. Along the accurate representation of the product info such as title, image, description etc… Also for landing page and data feed.

          1. Thank you Emmanuel, i will check, i think Opencart feed does not support INR ! thanks for your help 🙂

  7. Hi,
    How to present correct product microdata for product you have not fixed price (On quote). If set the price to 0.00 in the SERP it shows : “Free”, but it isn’t free, I would “on quote” or something like this (in french).

    Thanks

      1. If it is not a regular product page, but a solution page. There is no fixed price (the price can vary depending on needs), so how can I use microdata to show something like “on quote” instead of a price.

        1. Hello Cedric, thanks for the explanation. I fully understand now. This article is written for Google Shopping, where fixed prices are required. Microdata is read server side, meaning that the price needs to be there when the page is first loaded. So what you are trying to do would not work due to the way Google crawls pages. It can not select forms thus making a dynamic microdata non beneficial. However if you do want to do this, than you need to look at client side scripting such as javascript to input the correct price. But again this would be pointless as Google does not interact with javascript nor forms. So your solution has to be a base price needs to be pre selected server side. I recommend contacting your web developer. Hope it helps.

  8. Selina Destefano

    Think I have found the error….your code as sent to me reaads….

    I suspect “&it;” and “>” should have been “”‘

    Just a guess !! But it has fixed that error.

    HOWEVER NOTE THAT THE PRICE HAS NO DECIMAL POINT…..SHOWS 15975 but should be 159.75 !!
    (see Srceenshot)
    SD

    1. Hi, yes I removed that from my comment as the comments system removes any coding. I have added the updated code in this article. It strips out the currency and keeps both the comma and dot is present.

      1. Selina Destefano

        Can’t see the code you refer to.
        Code above is still the original code you had up earlier.
        Still produces the $ sign and gives an error……

        $59.95 (The property $59.95 is not a valid price specification. Learn more about http://schema.org/price.)

        1. Hi, the code in this article is updated, as the preg_replace code is not the same. If you do not see an updated code in this article, please refresh. This is tested now and correct. Reference to line 5

  9. Selina Destefano

    Hello again.
    Thanks for getting back to me. Have done as suggested.
    This code is giving me an error message. See screenshot.
    SD

  10. Selina Destefano

    Hi Emmanuel,
    Did as you suggested and imported the changes to product.tpl
    Then ran STructured Data Testing Tool.
    On website’s url it came back as “DETECTED”
    Running specific Product Landing Pages I get the same WARNING

    warningprice
    $299.00 (The property $299.00 is not a valid price specification. Learn more about http://schema.org/price.) See ScreenShot.

    Looks as if it does not like the “$” sign. Seems to be picking it up from OpenCart through (I assume) this line of code…..

    <meta itemprop="price" content="”>

    …….may be causing it !

    The other ScreenShot reflects the DATA page for OpenCart where PRICE is entered simply as NUMBERS (but as text) with no $ sign……

    Any suggestions for correcting this.

    Regards
    SD

    By the way…..Thanks for your response on GOOGLE Forum !!

  11. Natalia Farías

    Hello, I got this error with your code “La propiedad php if (!$special) { echo $price;}else{echo $special;} no es una especificación de precio válida.” on validator. “the property php if (!$special) { echo $price;}else{echo $special;} is not a valid price specification” I’m using Opencart 2.2

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.