skip to Main Content

I’m not really a coder, I’m broke and trying to earn money with online store. I’m starting an online store using OpenCart 2.3.0.2, and been trying to SEO optimize it before launching. This time, I’m trying to improve it with structured data markup. Since I don’t have the financial capacity (for now) to buy modules, I’m doing it myself and learning how stuffs works.

I am trying to create my own Schema for Product module, for OpenCart 2.3, but got stuck on the Review part. JSON-LD is fully built and tested.

I was trying to markup using JSON-LD, learned it from this page and from OpenCart forum, here’s a snippet of the problematic code (the review part):

"review": [
  <?php foreach($reviewss as $review) { ?>
  {
    "@type": "Review",
    "author": "<?php echo $review['author'];?>",
    "datePublished": "<?php echo $review['date_added'];?>",
    "description": "<?php echo $review['text'];?>",
    "name": "<?php echo $review['author'];?>",
    "reviewRating": {
    "@type": "Rating",
    "bestRating": "5",
    "ratingValue": "<?php echo $review['rating'];?>",
    "worstRating": "1"
    }
  } 
  <?php } ?>
]
}

Which produce the output:

  "review": [
  {
    "@type": "Review",
    "author": "A Parker",
    "datePublished": "16/12/2018",
    "description": "Wow! Product arrive yesterday and it's well packed. The product is well-designed.",
    "name": "A Parker",
    "reviewRating": {
    "@type": "Rating",
    "bestRating": "5",
    "ratingValue": "5",
    "worstRating": "1"
    }
  } #there should be a comma here after "}". 
  {
    "@type": "Review",
    "author": "David Lay",
    "datePublished": "15/12/2018",
    "description": "Great product! Works as advertised.",
    "name": "David Lay",
    "reviewRating": {
    "@type": "Rating",
    "bestRating": "5",
    "ratingValue": "5",
    "worstRating": "1"
    }
  } 
  ]
}


Here's the correct one. Tested using Google Structured Data Markup.


  "review": [
  {
    "@type": "Review",
    "author": "A Parker",
    "datePublished": "16/12/2018",
    "description": "Wow! Product arrive yesterday and it's well packed. The product is well-designed.",
    "name": "A Parker",
    "reviewRating": {
    "@type": "Rating",
    "bestRating": "5",
    "ratingValue": "5",
    "worstRating": "1"
    }
  },
  {
    "@type": "Review",
    "author": "David Lay",
    "datePublished": "15/12/2018",
    "description": "Great product! Works as advertised.",
    "name": "David Lay",
    "reviewRating": {
    "@type": "Rating",
    "bestRating": "5",
    "ratingValue": "5",
    "worstRating": "1"
    }
  } 
  ]
}

I’m not sure what its called actually, I think the – LOOP problem. There should be a comma to separate the review for each review post. The code works fine if there is only 1 review.

There should be a condition to insert the comma, like if review is more than one, then insert the comma, if not then no comma. Then ofcourse, the last item should have no comma.

I’ve been pulling my hair to solve this issue. I don’t know PHP, but I tried hard by reading forum, on the topic of loop and if/then condition, foreach etc. But can hardly understand it.

I’m not sure how to ask the question, maybe How to Loop Product Review Post in OpenCart 2.3 for JSON-LD Schema?
Any help is greatly appreciated.

2

Answers


  1. why not use json_encode()?

    the JSON-LD Product schema looks whole different:

    <script type="application/ld+json">
    {
      "@context": "http://schema.org",
      "@type": "Product",
      "aggregateRating": {
        "@type": "AggregateRating",
        "ratingValue": "3.5",
        "reviewCount": "11"
      },
      "description": "0.7 cubic feet countertop microwave. Has six preset cooking categories and convenience features like Add-A-Minute and Child Lock.",
      "name": "Kenmore White 17" Microwave",
      "image": "kenmore-microwave-17in.jpg",
      "offers": {
        "@type": "Offer",
        "availability": "http://schema.org/InStock",
        "price": "55.00",
        "priceCurrency": "USD"
      },
      "review": [{
          "@type": "Review",
          "author": "Ellie",
          "datePublished": "2011-04-01",
          "description": "The lamp burned out and now I have to replace it.",
          "name": "Not a happy camper",
          "reviewRating": {
            "@type": "Rating",
            "bestRating": "5",
            "ratingValue": "1",
            "worstRating": "1"
          }
        }, {
          "@type": "Review",
          "author": "Lucas",
          "datePublished": "2011-03-25",
          "description": "Great microwave for the price. It is small and fits in my apartment.",
          "name": "Value purchase",
          "reviewRating": {
            "@type": "Rating",
            "bestRating": "5",
            "ratingValue": "4",
            "worstRating": "1"
          }
      }]
    }
    </script>
    

    for example:

    $data = (object) array(
        "@context" => "http://schema.org",
        "@type" => "Product",
        "aggregateRating" => (object) array(
            "@type": "AggregateRating",
            "ratingValue" => "3.5",
            "reviewCount" => "11"
        ),
        "description" => "0.7 cubic feet countertop microwave. Has six preset cooking categories and convenience features like Add-A-Minute and Child Lock.",
        "name" => "Kenmore White 17" Microwave",
        "image"=> "kenmore-microwave-17in.jpg",
        "offers" => (object) array(
            "@type": "Offer",
            "availability": "http://schema.org/InStock",
            "price": "55.00",
            "priceCurrency": "USD"
        ),
        "review" => array()
    );
    
    foreach($reviews as $review) {
        array_push($data->review, $review);
    }
    
    echo '<script type="application/ld+json">';
    echo json_encode($data);
    echo '</script>';
    

    also see the structured data testing tool.

    Login or Signup to reply.
  2. Try this

    "review": [
    
      <?php foreach($reviews as $key => $review); { ?>
      {
        "@type": "Review",
        "author": "<?php echo $review['author'];?>",
        "datePublished": "<?php echo $review['date_added'];?>",
        "description": "<?php echo $review['text'];?>",
        "name": "<?php echo $review['author'];?>",
        "reviewRating": {
        "@type": "Rating",
        "bestRating": "5",
        "ratingValue": "<?php echo $review['rating'];?>",
        "worstRating": "1"
        }
      } 
      <?php if ($key != (count($reviews) - 1)){ ?>,<?php } ?><?php } ?>
    ]
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search