2

In Magento 2, I want to hide the default rich snippets. I already figured out how to remove the price and product rich snippets by reading this blog: http://blog.mageworx.com/2016/09/extending-rich-snippets-functionality-in-magento-2/

But if you have your reviews enabled in Magento, it will automatically create rich snippets for the rating and reviews. Does anybody know how to remove the structured data of the rating and reviews?

0

I've found a solution to hide the review rich snippets. In the file Vendor/Module/view/frontend/layout/catalog_product_view.xml, I've added the following code:

    <!--
        Replace the default review template file with a custom review template file
    -->
    <referenceContainer name="content">
        <referenceBlock name="product.info.product_additional_data">
            <action method="setTemplate" ifconfig="sytem_config/product/enable_product_structured_data">
                <argument name="template" xsi:type="string">Vendor_Module::product/view/list.phtml</argument>
            </action>
        </referenceBlock>
    </referenceContainer>

So based on the configuration 'enable_product_structured_data', which I've added in the system.xml, the list.phtml will be overwritten by the file Vendor/Module/view/frontend/template/product/view/list.phtml. That file is the same as Magento's own list.phtml (located in module-review), but without all the rich snippets attributes (itemscope, itemprop and itemtype).

To override the rating rich snippets, I had to create a rewrite. My di.xml contains the following code:

<preference for="Magento\Review\Block\Product\ReviewRenderer"
            type="Vendor\Module\Rewrite\Magento\Review\Block\Product\ReviewRenderer"/>

My rewrite file (Vendor/Module/Rewrite/Magento/Review/Block/Product/ReviewRenderer.php) contains the following code:

<?php

namespace Vendor\Module\Rewrite\Magento\Review\Block\Product;

use Vendor\Module\Helper\Config;

/**
 * Class ReviewRenderer
 * @package Vendor\Module\Rewrite\Magento\Review\Block\Product
 */
class ReviewRenderer extends \Magento\Review\Block\Product\ReviewRenderer
{
    /**
     * @var \Vendor\Module\Helper\Config
     */
    protected $configHelper;

    /**
     * ReviewRenderer constructor.
     * @param \Magento\Framework\View\Element\Template\Context $context
     * @param \Magento\Review\Model\ReviewFactory $reviewFactory
     * @param \Vendor\Module\Helper\Config $configHelper
     * @param array $data
     */
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Review\Model\ReviewFactory $reviewFactory,
        \Vendor\Module\Helper\Config $configHelper,
        array $data = []
    )
    {
        $this->configHelper = $configHelper;
        parent::__construct($context, $reviewFactory, $data);
    }

    /**
     * @param \Magento\Catalog\Model\Product $product
     * @param string $templateType
     * @param bool $displayIfNoReviews
     * @return string
     */
    public function getReviewsSummaryHtml(
        \Magento\Catalog\Model\Product $product,
        $templateType = \Magento\Review\Block\Product\ReviewRenderer::DEFAULT_VIEW,
        $displayIfNoReviews = false
    )
    {
        if (!$product->getRatingSummary() && !$displayIfNoReviews) {
            return '';
        }
        // pick template among available
        if (empty($this->_availableTemplates[$templateType])) {
            $templateType = self::DEFAULT_VIEW;
        }
        if ($this->configHelper->get(Config::ENABLE_PRODUCT_STRUCTURED_DATA) &&
            ($templateType == self::FULL_VIEW || $templateType == self::DEFAULT_VIEW)) {
            $this->setTemplate('Vendor_Module::helper/summary_custom.phtml');
        } else {
            $this->setTemplate($this->_availableTemplates[$templateType]);
        }

        $this->setDisplayIfEmpty($displayIfNoReviews);

        if (!$product->getRatingSummary()) {
            $this->_reviewFactory->create()->getEntitySummary($product, $this->_storeManager->getStore()->getId());
        }
        $this->setProduct($product);

        return $this->toHtml();
    }
}

Then I created the file Vendor/Module/view/frontend/templates/helper/summary_custom.phtml. This is also the same as Magento's own summary.pthml (also located in module-review), but again without the rich snippets attributes.

In this way, I was able to remove Magento's default rich snippets and add my own structured data.

| improve this answer | |

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.