Currently, Magento allows you to use the Product related information to set as meta description in configuration settings but let’s say if you wish to use short description, then?
Well, while working on a project I faced the above situation and fixed it like a pro 🙂
If no Short Description is set in the product’s information use Magento’s configuration settings for the Meta Description.
First, you have to add the below code in [Vendor Name]/[Module Name]/etc/events.xml in your existing module or create a new module:
1 2 3 4 5 6 7 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd"> <!-- Product Meta Tags Changes Action --> <event name="catalog_controller_product_view"> <observer name="product__meta_observer" instance="[Vendor Name]\[Module Name]\Observer\SeoMetaObserver"/> </event> </config> |
After that, you have to create a new observer file which you have mention in events.xml file like:
[Vendor Name]\[Module Name]\Observer\SeoMetaObserver.php
And add below code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
<?php namespace [Vendor Name]\[Module Name]\Observer; use Magento\Framework\Event\ObserverInterface; class SeoMetaObserver implements ObserverInterface { const XML_PRODUCT_AUTO_METADESCRIPTION = 'catalog/fields_masks/meta_description'; /** * @var \Magento\Framework\App\Config\ScopeConfigInterface */ protected $scopeConfig; /** * SeoMetaObserver constructor. * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig */ public function __construct( \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig ) { $this->scopeConfig = $scopeConfig; } public function execute(\Magento\Framework\Event\Observer $observer) { $product = $observer->getProduct(); $metaDesc = trim($product->getMetaDescription()); if ($metaDesc == '') { if ($product->getShortDescription() != '') { $metaDesc = $product->getShortDescription(); //If no SEO Meta Description is set in the product’s information use the Short Description } else { $configMeta = $this->scopeConfig->getValue(self::XML_PRODUCT_AUTO_METADESCRIPTION, \Magento\Store\Model\ScopeInterface::SCOPE_STORE); //If no Short Description is set in the product’s information use configuration settings for the Meta Description $string = str_replace("{{","",str_replace("}}","",$configMeta)); $finalMeta = explode(' ', $string); $i = 0; foreach ($finalMeta as $meta) { if ($i == 0) { $metaDesc .= $product->getData($meta); } else { $metaDesc .= ' ' . $product->getData($meta); } $i++; } } } $product->setMetaDescription($metaDesc); } } |
Then flush the Magento cache and check the product page meta description tag.
If you want get query string params in controller file,...
Create di.xml and add the below code Magemonkey/Redirect/etc/frontend/di.xml [crayon-62876b8db7dc4610050942/] Create...
You can try below code to change local date to...
Step 1: First you need to add registration.php file in...
Step1 : Override message.js in current theme file on the...