1

I am trying to automatically add the current year to the meta title and description in for example category-pages. I want to achieve something like this:

Meta title: "The best category items for {{year}}" >> "The best category items for 2019"

What would be the easiest way to do this?

  • for a particular category or all categories? – Amit Bera Jun 3 at 12:59
  • For all categories in which the {{year}} tag is used. I just want to replace this tag with the current year. – klaaskox Jun 3 at 13:11
0

In this case, you can use event/observer.

Fire an observer on the event catalog_category_load_after at frontend area set meta description on the fly.

Create events.xml at app/code/StackExchange/Magento/etc/frontend/ and as `events.xml location under on frontend and this event only fire for frontend area.

events.xml code

<?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="catalog_category_load_after">
        <observer instance="StackExchange\Magento\Observer\Frontend\Catalog\CategoryLoadAfter" 
                  name="stackexchange_magento_observer_frontend_catalog_categoryloadafter_catalog_product_load_after"
        />
    </event>
</config>

Create Observer class CategoryLoadAfter at app/code/StackExchange/Magento/Observer/Frontend/Catalog/.

CategoryLoadAfter.php code

<?php
namespace StackExchange\Magento\Observer\Frontend\Catalog;

class CategoryLoadAfter implements \Magento\Framework\Event\ObserverInterface {

    /**
     * @var \Psr\Log\LoggerInterface
     */
    private $logger;

    public function __construct(
     \Psr\Log\LoggerInterface $logger
    ) {

        $this->logger = $logger;
    }
    /**
     * Execute observer
     *
     * @param \Magento\Framework\Event\Observer $observer
     * @return void
     */
    public function execute(
        \Magento\Framework\Event\Observer $observer
    ) {
        $this->logger->debug(__METHOD__);
        $category = $observer->getEvent()->getCategory();
        if ($category instanceof \Magento\Catalog\Model\Category) {
            $oldMetaTitle = $category->getMetaTitle();
            $oldMetaDescription = $category->getMetaDescription();
            $oldMetaKeyword = $category->getMetaKeywords();
            $category->setMetaTitle(str_replace("{{year}}",date('Y'),$oldMetaTitle));
            $category->setMetaDescription(str_replace("{{year}}",date('Y'),$oldMetaDescription));
            $category->setMetaKeywords(str_replace("{{year}}",date('Y'),$oldMetaKeyword));
        }
    }

}

Also, your module must have:

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

After adding the event you should flush the cache.

  • Thanks a lot, works great! – klaaskox Jun 3 at 14:54

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.