6

I want to set meta title , description by the help of observer.
But unable to set data here. please see my code:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$_page_config = $objectManager->get('Magento\Framework\View\Page\Config');

echo $_meta_title = strip_tags($_page_config->setTitle("my custom title"));
echo $_meta_description = strip_tags($_page_config->setDescription("my custom description"));

Anyone has an idea would me much appreciated.

1 Answer 1

0

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]\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:

<?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.

Reference: https://www.magemonkeys.com/magento-2-product-meta-description-override-how-to-use-short-description-instead-of-products-information/

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.