Magento 2 – Customize and Override Default Meta Tags for Better SEO

magento2meta-tagsseo

in my product pages, my meta description tag is taking the product description despite the configurations that I set in Store > Config > Catalog > Catalog, where I set the description to {{short_description}}, but I always get the long-description content on the

<meta name="description" content="...">

Beyond that, there is the <meta property="og:description" content="Short Description.">, which always get the short description, I've tried to reidex, clean cache, delete static files, recompile, etc, but magento seems to ignore my configs. So I've searched to other ways to do that, overriding or something, and I find this question, but this doesn't appear to be a magento 2 solution.

So, any ideas of how can I override the default magento 2 meta description tags?

Thanks in advance!

Best Answer

You will do some customization for that. Using event Observer you change Product meta description as per your requirement i giving one observer event its usefull for that.

<event name="controller_action_postdispatch">
        <observer name="vendor_name_module_name_observer_meta" instance="Vendor\Module\Observer\SeoMetaObserver"/>
</event>

namespace Vendor\Module\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Catalog\Model\Resource\Product\CollectionFactory;
use Magento\UrlRewrite\Model\UrlRewriteFactory as BaseUrlRewrite;

class SeoMetaObserver implements ObserverInterface
{
    protected $urlRewriteFactory;
    protected $registry;
    protected $design;
    protected $context;
    protected $objectManager;
    protected $storeManager;

    public function __construct(
        BaseUrlRewrite $rewriteFactory,
        \Magento\Framework\Registry $registry,
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Framework\ObjectManagerInterface $objManager,
        \Magento\Catalog\Helper\Output $catalogOutput
    ) {
        $this->storeManager = $context->getStoreManager();
        $this->registry = $registry;
        $this->request = $context->getRequest();
        $this->design = $context->getDesignPackage();
        $this->context = $context;
        $this->objManager= $objManager;
        $this->catalogOutput = $catalogOutput;
        $this->urlRewriteFactory = $rewriteFactory;
    }
    protected function getProductCollection()
    {
        $currentProduct = $this->registry->registry('current_product');
        return $currentProduct;
    }
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $eventData = $observer->getData();
        if ($this->request->getFullActionName() == 'catalog_product_view') {
            $currentProduct = $this->registry->registry('current_product')->getId();
            $title = $productname;
            $productdescription = $description;
            $productshortdescription = $shortdescription;
            $productfulldescription = $fulldescription;
            $layout = $this->context->getLayout();
            $pageMainTitle = $layout->getBlock('page.main.title');
            $pageMainTitle->setPageTitle("productname");
            $currentProduct->setDescription("product_description");
            $currentProduct->setShortDescription("short_description");
            $currentProduct->setFullDescription("full_descriptiom");
        }
    }
}
Related Topic