2

I have to extend default rich snippets for products. For this I have my script in .phtml file as:

<script type="application/ld+json">
{
"@context": "http://schema.org/",
"@type": "Product",
<?php if ($block->nameEnabled()) { ?>
"name": "<?php echo $_products->getName(); ?>",
<?php } ?> <?php if ($block->imageEnabled()>0) { ?>
"image": [
<?php if($block->imageEnabled()==1) { ?>
"<?php echo $imageurl['0'] ?>"
<?php } ?>
<?php if ($block->imageEnabled()==2) { ?>
"<?php echo $imageurl['1'] ?>"
<?php } ?>
<?php if ($block->imageEnabled()==3){ ?>
"<?php echo $imageurl['2'] ?>"
<?php } ?>
], <?php } ?>
<?php if ($block->desEnabled()==1) { ?>
"description": "<?php echo $_products->getShortDescription(); ?>",
<?php } ?>
<?php if ($block->desEnabled()==2) { ?>
"description": "<?php echo $_products->getMetaDescription(); ?>",
<?php } ?>
<?php if ($block->getBrand()) { ?>
"brand": {
        "@type": "Thing",
        "name": "<?php echo $brandName = $block->getBrand();?>" 
    }, <?php } ?>
    <?php if ($block->skuEnabled()) { ?>
"sku": "<?php echo $_products->getSku(); ?>" ,<?php } ?>

"offers": {
<?php if ($block->priceEnabled()) { ?>
"@type": "Offer",
<?php if ($block->currencyEnabled()) { ?>
"priceCurrency": "<?php echo $block->getCurrencyCode(); ?>",<?php } ?>
"price": "<?php echo $_products->getFinalPrice(); ?>"
<?php } ?>
}
}
</script>

In which referenceContainer this block should be added so that snippets can be seen in https://search.google.com/structured-data/testing-tool/

0

If you are using json, ‘head.additional’ will work.

  • @RamaChandranM This is a direct and exact answer to the question. – Shawn Abramson Aug 7 '18 at 3:24
  • Thanks @shawnfor a reply. Human error always occurs :(. I think you may be explain some detail it will helpful for other :) – Rama Chandran M Aug 7 '18 at 5:56
  • Thanks for response @ShawnAbramson. I used "head.additional" but Google structure data tool cannot read snippets. On the other hand, i used "content" referenceContainer and Structure data testing tool is reading richsnippets. But I have read in Google developers guide that richsnippets should be added in header of the page. So which will be the right referenceContainer to implement this ? – Ajwad Syed Aug 7 '18 at 16:47
  • 1
    This should work, I've used this block quite a bit with no issues. I assume that you flushed the cache before checking right? Could you post the XML that you are using to update the layout with your code? – Shawn Abramson Aug 7 '18 at 16:48
  • 1
    default.xml will make it work for all pages. catalog_product_view.xml only works on product pages. – Shawn Abramson Aug 17 '18 at 19:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.