0

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!

2 Answers 2

1

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");
        }
    }
}
2
  • Thanks for answering, it has given me a direction to do that, but actually I did it a bit different, I'll post my answer below, but really thanks
    – lpFranz
    Aug 30, 2018 at 19:16
  • This is not working.
    – srokatonie
    Feb 12, 2021 at 19:35
2

I've followed the @Tushar answer, but make it a bit different, lets see how to do that:

I've created this event:

<event name="catalog_controller_product_view">
    <observer name="my_custom_metatags" instance="Vendor\Metatags\Observer\Product\CustomMetatags" />
</event>

With it, our Observer is only called on product view page, which is what I want.

And my Observer (I don't need to inject anything, so without constructor)

<?php
...

class CustomMetatags implements ObserverInterface {

public function execute(Observer $observer){
    $product = $observer->getProduct();
    // trates meta-description
    if (!$product->getMetaDescription()) {
        $short = $product->getShortDescription();
        if (!$short) {
            $short = $product->getDescription();
        } else {
            $short = $product->getName().' - '.$short;
        }
        $product->setMetaDescription($short);
    }

    // trates ALT atribute in images
    $images = $product->getMediaGalleryImages();
    $count = 1;
    foreach ($images as $image) {
        if(!$image->getLabel()) {
            $image->setLabel('Image '.$count.' - '.$product->getName());
            $count++;
        }
    }
}

}

And that's all! With this, my meta-description first attempt to get the proper field meta_description, if this is not filled in our product the second field to attempt is short_description and if it is not filled too, finally gets the description field content to set in meta-description tag.

The same with the images, if the label is not filled, then we add a custom label with product name and a counter.

I hope it helps someone in future, thanks to Tushar to show me the way.

1
  • It it is usefull solution please upvoted and right them.
    – Tushar
    Aug 31, 2018 at 6:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.