Hiding the Default Rich Snippets Functionality in Magento 2

0
17346
Reading Time: 3 minutes

If you’ve come to this page, you must know what Rich Snippets are and how they work in Magento 2.

Upon the whole, the structured data markup is quite a cool feature of the second version of the eCommerce platform. Schema.org was added as the default template, which lets merchants equip their site search results with price, rating, reviews, or other markup values in just a few clicks.

There are, however, some cases when you may want to disable the default rich snippets functionality. The reasons for that can be:

  • price format issues — currently, the format used by Magento 2 doesn’t correspond to the latest schema.Org requirements, which may trigger the following error:1
  • conflicts with some 3-d party structured data markup extensions,
  • issues with Google products — this type of products may come with a different price (e.g. MSRP), which, in turn, triggers conflicts with the default Magento 2 rich snippets.

Unfortunately, up to v.2.1.1, it’s impossible to disable the default rich snippets functionality via the Config settings. Hence, you need some workaround solution.

In this article, I’ll tell you how to disable the default structured data markup via creating a a little, yet very useful extension for Magento 2.

First, let’s create the extension’s environment.

  1. Creating the Extension

Let’s call it ‘Owner_Custom’  (although, you can choose any name you wish).

Next, go to app/code and create an ‘Owner’ folder there. Inside of this folder, create a new one, named ‘Custom’ — it will be the root folder for our extension.

From in here, we create the following files:

FILE: registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Owner_Custom',
    __DIR__
);

FILE: etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Owner_Custom" setup_version="0.0.1"/>
</config>

At the next step, install the extension via Console:

php bin/magento setup:upgrade  — keep-generated 

All done, now our extension can be found on the list of all modules under the Admin panel:

php bin/magento module:status 

2. Removing the default data markup

When the extension’s environment is ready, we can get down to creating a layout file, where all the magic takes place.

FILE: view/frontend/layout/catalog_product_view.xml

<?xml version="1.0"?>
<page xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <attribute name="itemtype" remove="true" ifconfig="mageworx_seo/markup/product/rs_enabled"/>
        <attribute name="itemscope" remove="true" ifconfig="mageworx_seo/markup/product/rs_enabled"/>
    </body>
</page>

IMPORTANT

Don’t forget to update cache each time you make change to the file.

In this part of the code, we are going to change the parameters that are used for formatting structured data markup. After we implement this, the markup will contain only our offer, e.g.:

2

As you can see, after this manipulation, Google stops throwing any warnings in the ‘Offer’ section.

Let’s add some more code lines:

<?xml version="1.0"?>
<page xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <attribute name="itemtype" remove="true"/>
        <attribute name="itemscope" remove="true"/>
        <referenceBlock name="product.price.final">
            <arguments>
                <argument name="zone" xsi:type="boolean">false</argument>
            </arguments>
        </referenceBlock>
    </body>
</page>

By adding these piece, we’ve removed the parameter that was responsible for marking up this block as an ‘Offer’. As a result, Google tools don’t detect any data that could be used for Rich Snippets here:

3

And there is one final touch left to do.

In order to disable the default rich snippets for all product types, you need to add 2 more files. This one for downloadable products:

FILE: view/frontend/layout/catalog_product_view_type_downloadable.xml

<?xml version="1.0"?>
<page xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="product.price.final.copy">
            <arguments>
                <argument name="zone" xsi:type="boolean">false</argument>
            </arguments>
        </referenceContainer>
    </body>
</page>

And this is for bundle products:

FILE: view/frontend/layout/catalog_product_view_type_bundle.xml

<?xml version="1.0"?>
<page xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="product.price.render.bundle.customization">
            <arguments>
                <argument name="zone" xsi:type="boolean">false</argument>
            </arguments>
        </referenceBlock>
    </body>
</page>

IMPORTANT

Note that if some of the products in your Magento store has already been reviewed, this review will be included into the default markup. This may trigger the following error:

31e6d408642cc8cc288f6fde6f6fb0d8
Since it’s hardcoded in the Magento theme files, to delete it, you have to rewrite this file:

vendor/magento/module-review/view/frontend/templates/helper/summary.phtml

Copy the file content into:

app/design/frontend/[package]/[theme]/templates/helper/summary.phtml

and delete the markup:

(itemprop=“aggregateRating” itemscope itemtype=“https://schema.org/AggregateRating” ; itemprop=“bestRating” ; itemprop=“ratingValue” ; itemprop=“reviewCount” )

As a result, you should get this (for Magento 2.1x) :

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

// @codingStandardsIgnoreFile

?>
<?php $url = $block->getReviewsUrl() . '#reviews'; ?>
<?php $urlForm = $block->getReviewsUrl() . '#review-form'; ?>
<?php if ($block->getReviewsCount()): ?>
<?php $rating = $block->getRatingSummary(); ?>
<div class="product-reviews-summary<?php echo !$rating ? ' no-rating' : ''?>">
    <?php if ($rating):?>
    <div class="rating-summary">
         <span class="label"><span><?php /* @escapeNotVerified */ echo __('Rating') ?>:</span></span>
         <div class="rating-result" title="<?php /* @escapeNotVerified */ echo $rating; ?>%">
             <span style="width:<?php /* @escapeNotVerified */ echo $rating; ?>%"><span><span><?php /* @escapeNotVerified */ echo $rating; ?></span>% of <span>100</span></span></span>
         </div>
     </div>
    <?php endif;?>
    <div class="reviews-actions">
        <a class="action view" href="<?php /* @escapeNotVerified */ echo $url ?>"><span><?php /* @escapeNotVerified */ echo $block->getReviewsCount() ?></span>&nbsp;<span><?php /* @escapeNotVerified */ echo($block->getReviewsCount() == 1) ? __('Review') : __('Reviews') ?></span></a>
        <a class="action add" href="<?php /* @escapeNotVerified */ echo $urlForm ?>"><?php /* @escapeNotVerified */ echo __('Add Your Review') ?></a>
    </div>
</div>
<?php elseif ($block->getDisplayIfEmpty()): ?>
<div class="product-reviews-summary empty">
    <div class="reviews-actions">
        <a class="action add" href="<?php /* @escapeNotVerified */ echo $urlForm; ?>">
            <?php /* @escapeNotVerified */ echo __('Be the first to review this product') ?>
        </a>
    </div>
</div>
<?php endif; ?>

And finally, this is how you can enable/disable our custom extension:

php bin/magento module:enable Owner_Custom
php bin/magento module:disable Owner_Custom

Ah, and don’t forget to refresh cache to make all the changes take into effect.

That’s basically it.

Should you have any questions on this topic, feel free to leave your comments in the section below.

LEAVE A REPLY

Please enter your comment!
Please enter your name here