2

From admin, I'm trying to insert category name as a variable in metatags (something like {category name} in meta description).

I go to admin: Products -> Categories. From the details of Category, In Search Engine Optimization dropdown., I am adding Meta Description which includes category name like in curly brackets {category name}. But, Im not able to do..

Not sure Im doing it right, Is there any way or certain variable for Category name, which we can insert like this.

2
  • You can do this using magento event observer. Apr 27, 2018 at 14:08
  • how can we do it_ Can you give me an example to add same metatags with individual category names in it
    – Md ritzwan
    Apr 27, 2018 at 18:44

1 Answer 1

0

You can do this things with custom extension.

  1. app\code\Vendor\Extension\etc\events.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="catalog_category_save_after">
          <observer name="catalog_metadescription" instance="Vendor\Extension\Observer\Categorysaveafter" />
       </event>
    </config>
    
  2. Create another file at this location.

    app\code\Vendor\Extension\Observer\Categorysaveafter.php
    
    <?php
    namespace Vendor\Extension\Observer;
    
    use Magento\Catalog\Model\Category;
    use Magento\Framework\Event\ObserverInterface;
    
    class Categorysaveafter implements ObserverInterface
    {
       public function execute(\Magento\Framework\Event\Observer $observer)
       {
           //THIS CALLED WHEN YOU SAVE ANY CATEGORY
           // DO YOUR CODE TO GET CATEGORY NAME AND SET IN META
       }
    }
    
3
  • Thanks Dhiren, Just one more thing, I had a Category for which I added its meta title from backoffice. And I want to use this metatitle in meta description, Is there any way to extract this meta title.. I tried with same code you provided initiating an instance, but it returns Magento default title - (which is not the one, I stored via backoffice)
    – Md ritzwan
    May 1, 2018 at 12:20
  • You need to debug that, why you don't get those store meta title. May 1, 2018 at 13:25
  • If my answer help you, please accept it, So it will help comminity as well. May 1, 2018 at 13:26

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.