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-&gt;registry = $registry;
    $this-&gt;request = $request;
    $this-&gt;layoutFactory = $layoutFactory;
    $this-&gt;logger = $logger;
}

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

}

CodeForGood
  • 764
  • 7
  • 31

2 Answers2

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"

Prachi Saxena
  • 564
  • 2
  • 10
  • Even after correcting the class name in xml file, it is still not working. – CodeForGood May 13 '22 at 09: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. – CodeForGood May 13 '22 at 10:14
  • Hi Dhiren, could you please have a look at the related post here https://magento.stackexchange.com/questions/358323/magento-2-4-2-unable-to-process-binding-ifnot-functionreturn-customer-fu I'm struggling to fix this for over 10 days now. I added as much information about the problem as possible. – CodeForGood Jul 27 '22 at 18:51
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.

Amitkumar solanki
  • 875
  • 1
  • 7
  • 20
  • Hi Dhiren, could you please have a look at the related post here https://magento.stackexchange.com/questions/358323/magento-2-4-2-unable-to-process-binding-ifnot-functionreturn-customer-fu I'm struggling to fix this for over 10 days now. I added as much information about the problem as possible. – CodeForGood Jul 27 '22 at 18:50