1

I need to change my meta tag to NOINDEX and NOFOLLOW for particular page (especially in product detail page) in magento 2. I have tried adding

<head>
    <meta name="robots" content="NOINDEX,NOFOLLOW"/>
</head>

and also

<reference name="head">
    <action method="setRobots"><value>NOINDEX,NOFOLLOW</value></action>
 </reference>

In layout update xml but not changing from default how to fix the issue.

P.S.:I need to update via magento 2 backend.

3 Answers 3

1

To resolve this issue, you can use event/Observer.

Create a small module.

Fire an Observer an observer on layout_load_before event. On this event make product details page is NOINDEX,NOFOLLOW.

Event.xml:

events.xml is located at app/code/{Vendor}/{Modulename}/etc/frontend/ code should be like that

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="layout_load_before">
        <observer name="add_robot" instance="{Vendor}\{Modulename}\Observer\NoindexfollowOnProduct" />
    </event>    
</config>

Observer class

NoindexfollowOnProduct.php is located at app/code/{Vendor}/{Modulename}/Observer and the code should be like that

<?php
namespace {Vendor}\{Modulename}\Observer;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

class NoindexfollowOnProduct implements ObserverInterface
{
    protected $request;

    protected $layoutFactory;

    public function __construct(
        \Magento\Framework\App\Request\Http $request,
        \Magento\Framework\View\Page\Config $layoutFactory
        ) {
            $this->request = $request;
            $this->layoutFactory = $layoutFactory;
    }
    public function execute(Observer $observer)
    {
        $fullActionName = $observer->getFullActionName();
        /* Check Current page  by full action */
        if ($fullActionName == "catalog_product_view"){
                $this->layoutFactory->setRobots('NOINDEX,NOFOLLOW');
        }

    }

}

Also, this module should have:

  1. app/code/{Vendor}/{Modulename}/etc/module.xml.
  2. app/code/{Vendor}/{Modulename}/composer.json
  3. . app/code/{Vendor}/{Modulename}/registration.json.

After adding the event you should flush the cache.

2
  • I need to add NOINDEX NOFOLLOW Only for selected product pages not all. Is that possible. Thanks Apr 10, 2018 at 10:20
  • Yes, you can do for product wish. Inside if ($fullActionName == "catalog_product_view") you can get product object by $observer->getLayout()->getBlock('product.info')-.>getProduct()
    – Amit Bera
    Apr 10, 2018 at 10:35
1

If you want to set "NOINDEX,NOFOLLOW" on product level check this extension for details: https://github.com/28software/module-seo

I create product attribute 'meta_robots'. Depending on the value it changes <meta name="robots" content="{Chosen attribute value}"/>

Here is the main logic:

<?php
/**
 * Copyright © 28Software (https://28software.com)
 * See LICENSE for the license details.
 */
declare(strict_types = 1);

namespace TESoftware\Seo\Plugin;

use Magento\Framework\App\Request\Http;
use Magento\Framework\Registry;
use Magento\Framework\View\Page\Config as PageConfig;
use Magento\Framework\View\Page\Config\RendererInterface;
use TESoftware\Seo\Model\Config\Source\MetaRobots as MetaRobotsSource;

class AddProductMetaRobots
{
    private Http $request;    
    private PageConfig $pageConfig;
    private Registry $registry;

    private const META_ROBOTS = [
        MetaRobotsSource::INDEX_FOLLOW     => 'INDEX,FOLLOW',
        MetaRobotsSource::NOINDEX_FOLLOW   => 'NOINDEX,FOLLOW',
        MetaRobotsSource::INDEX_NOFOLLOW   => 'INDEX,NOFOLLOW',
        MetaRobotsSource::NOINDEX_NOFOLLOW => 'NOINDEX,NOFOLLOW',
    ];

    public function __construct(
        Http $request,
        PageConfig $pageConfig,
        Registry $registry
    ) {
        $this->request = $request;
        $this->pageConfig = $pageConfig;
        $this->registry = $registry;
    }

    /**
     * @param RendererInterface $subject
     *
     * @return array
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function beforeRenderMetadata(RendererInterface $subject): array
    {
        if ($this->getFullActionName() === 'catalog_product_view') {
            $product = $this->registry->registry('current_product');
            $metaRobotsKey = (int)$product->getData('meta_robots');
            if (isset(self::META_ROBOTS[$metaRobotsKey])) {
                $this->pageConfig->setRobots(self::META_ROBOTS[$metaRobotsKey]);
            }
        }

        return [];
    }

    /**
     * Get full action name
     *
     * @return string
     */
    public function getFullActionName()
    {
        return $this->request->getFullActionName();
    }
}
0
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <head>
        <meta name="robots" content="NOINDEX,NOFOLLOW"/>
    </head>
</page>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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