0

My objective is to set NO INDEX, NO FOLLOW to a specific category page in an observer class. Below is my event.xml

<?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="sales_order_place_after">
        <observer name="Perfectmakeupmirrors_sales_order_place_after" instance="Perfectmakeupmirrors\Order\Observer\OrderPlaceAfter" />
    </event>
    <event name="sales_order_save_after">
        <observer name="Perfectmakeupmirrors_sales_order_save_after" instance="Perfectmakeupmirrors\Order\Observer\OrderSaveAfter" />
    </event>
    <event name="layout_load_before">
        <observer name="add_robot_page" instance="Perfectmakeupmirrors\Order\Observer\SetRobotsMetaTag" />
    </event> 
</config>

Here is the observer class where I try to first find out if it is the category page and if so, find out if it contains the word "part" in the title and set it to noindex and nofollow here. Observer class doesn't seem to be called since the debugging doesn't log the text in debug.log. Please let me know if there is anything wrong with event itself.

<?php
  
namespace Perfectmakeupmirrors\Order\Observer;
use Magento\Framework\Event\ObserverInterface;
use \Magento\Framework\Event\Observer;
use Magento\Framework\Registry;
use \Psr\Log\LoggerInterface;

class SetRobotsMetaTag implements ObserverInterface
{
    protected $request;
    protected $registry;
    protected $layoutFactory;
    protected $logger;

    public function __construct(
        \Magento\Framework\App\Request\Http $request,
        \Magento\Framework\Registry $registry,
        \Magento\Framework\View\Page\Config $layoutFactory,
        LoggerInterface $logger)

    {
        $this->registry = $registry;
        $this->request = $request;
        $this->layoutFactory = $layoutFactory;
        $this->logger = $logger;
    }

    public function execute(Observer $observer) {
        $this->logger->debug("Observer is alert");
        if ($this->request->getActionName() == 'category') { 
            $category = $this->registry->registry('current_category');
            $categoryName = $category->getName();
            if (stripos($categoryName, 'part') !== false) { 
                $this->layoutFactory->setRobots('NOINDEX,NOFOLLOW');
            } 
        }
    }
}

2 Answers 2

1

The event name is correct and it should work. It is better to debug in where "layout_load_before" event is defined which is shown below.

vendor/magento/framework/View/Layout/Builder.php: loadLayoutUpdates()

Set xdebug breakpoints in loadLayoutUpdates method and your event function which will give you proper suggestion.

Hope it can help you.

1

The class name you mentioned in events.xml is different from the one you uploaded. Please change events.xml to include instance as below

instance="Perfectmakeupmirrors\Order\Observer\SetRobotsMetaTag"

2
  • Even after correcting the class name in xml file, it is still not working. May 13 at 9:58
  • Is the event name correct? I ask this because the debug log in observer class doesn't do anything giving me an impression that event itself might not be correct. May 13 at 10: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.