0

I have the following code, which I have appended to the opengraph/general.phtml file in Magento 2.

I am hoping this will add the rich snippet data required by Google, but I am receiving an error (blank page).

Existing Code of opengraph/general.phtml

    <?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

/** @var $block \Magento\Catalog\Block\Product\View */
?>

<meta property="og:type" content="product" />
<meta property="og:title" content="<?= $block->escapeHtmlAttr($block->stripTags($block->getProduct()->getName())) ?>" />
<meta property="og:image" content="<?= $block->escapeUrl($block->getImage($block->getProduct(), 'product_base_image')->getImageUrl()) ?>" />
<meta property="og:description" content="<?= $block->escapeHtmlAttr($block->stripTags($block->getProduct()->getShortDescription())) ?>" />
<meta property="og:url" content="<?= $block->escapeUrl($block->getProduct()->getProductUrl()) ?>" />
<?php if ($priceAmount = $block->getProduct()->getFinalPrice()):?>
    <meta property="product:price:amount" content="<?= /* @escapeNotVerified */ $priceAmount ?>"/>
    <?= $block->getChildHtml('meta.currency') ?>
<?php endif;?>

<?php /** Rich Snippet Code */ ?>

    <?php
$_product = $this->getProduct();
$_brand = $this->getBrand($_product);
$_sku = $this->getSku($_product); 
?>

<script type="application/ld+json">
    {
        "@context": "https://schema.org/",
        "@type": "Product",
        "name": "<?php /* @escapeNotVerified */ echo $block->escapeQuote($block->stripTags($_product->getName())); ?>",
        "image": "<?php /* @escapeNotVerified */ echo $block->stripTags($block->getImage($_product, 'product_base_image')->getImageUrl()); ?>",
        "description": "<?php /* @escapeNotVerified */ echo $block->escapeQuote($block->stripTags($block->getDescription($_product))); ?>",
<?php if (strlen(trim($_brand))): ?>
        "brand": {
            "@type": "Thing",
            "name": "<?php echo $block->stripTags($_brand); ?>"
        },
<?php endif; ?>
        "offers": {
            "@type": "Offer",
            "priceCurrency": "<?php echo $block->getCurrencyCode() ?>",
            "url": "<?php echo $block->stripTags($block->getCurrentUrl($_product)); ?>",
<?php if ($_product->isAvailable()): ?>
            "availability": "https://schema.org/InStock",
 <?php else : ?>
            "availability": "https://schema.org/OutOfStock",
<?php endif; ?>
            "price": "<?php echo $this->getPrice(); ?>",
            "priceValidUntil": "<?php echo $_product->getSpecialToDate(); ?>"
        }
    }
</script>

Can anyone please advise a solution?

1

Are you sure these bits are okay

$_product = $this->getProduct();
$_brand = $this->getBrand($_product);
$_sku = $this->getSku($_product); 

I would have expected something like this

$_product = $block->getProduct();
$_brand = $_product->getBrand();
$_sku = $_product->getSku(); 

But ideally need to see errors in error log

  • That was exactly the issue Dominic. Thank you. – YorkieMagento Jun 10 '19 at 10:12

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.