Magento 2 : How to Insert Category Name as Meta-Description of Each Category From Admin
-
08-01-2021 - |
Question
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.
Solution
You can do this things with custom extension.
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>
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 } }
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange