We sacrifice by not doing any other technology, so that you get the best of Magento.

We sacrifice by not doing any other technology, so that you get the best of Magento.

    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:

    <?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]ObserverSeoMetaObserver"/>
        </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]ObserverSeoMetaObserver.php

    And add below code:

    <?php
    namespace [Vendor Name][Module Name]Observer;
    
    use MagentoFrameworkEventObserverInterface;
    
    class SeoMetaObserver implements ObserverInterface
    {
        const XML_PRODUCT_AUTO_METADESCRIPTION = 'catalog/fields_masks/meta_description';
        /**
         * @var MagentoFrameworkAppConfigScopeConfigInterface
         */
        protected $scopeConfig;
        
        /**
         * SeoMetaObserver constructor.
         * @param MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig
         */
        public function __construct(
            MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig
        )
        {
            $this->scopeConfig = $scopeConfig;
        }
        
        public function execute(MagentoFrameworkEventObserver $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, MagentoStoreModelScopeInterface::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.

    field_5bfb909c5ccae

      Get a Free Quote