在 Magento 站点的类别页面中设置 noindex nofollow

Setting noindex nofollow in a category page of Magento site

下面的观察者 class,我正在尝试在类别页面上设置 noindex 和 nofollow。我无法获取代码来首先检查它是否是类别页面,然后如果类别页面的标题在类别名称中包含“部分”则设置 noindex。你能帮忙找出来吗?

<?php

namespace Perfectmakeupmirrors\PmmHead\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 watching");
        $this->logger->debug("Action Name = " . $this->request->getActionName());
        $this->layoutFactory->setRobots('NOINDEX,NOFOLLOW');
        if ($this->request->getActionName() == 'category') { 
            $category = $this->registry->registry('current_category');
            $categoryName = $category->getName();
            $this->logger->debug("Category Name = $categoryName");
            if (stripos($categoryName, 'part') !== false) { 
                $this->logger->debug("Robots Set");
                $this->layoutFactory->setRobots('NOINDEX,NOFOLLOW');
            } 
        }
    }
}

更新您的功能条件。

 public function execute(Observer $observer) {
    $category = $this->registry->registry('current_category');
    $categoryName = $category->getName();
    $categoryName = trim($categoryName);
    $this->layoutFactory->setRobots('INDEX,FOLLOW');
    if (stripos($categoryName, 'Parts') !== false) {
      $this->layoutFactory->setRobots('NOINDEX,NOFOLLOW');
    }
}