We currently have meta data set up in the individual products. However, I would create an automated meta description for all products (including backdating old products).

I know I could do this through import/export - but I want something that is automated going forward.

For example, my product meta description should be '[brand], [product-name], buy online today.'

I can find how to do this for opengraph meta content but not meta descriptions and key words.

Can anyone please assist?

  • 1
    you may create product save after observer and update meta description based on above formate you mentioned. and for existing products you can create script which can load product and take brand - product name data and update meta description. Commented May 28, 2019 at 11:48
  • You want to show meta descriptions and key words ` '[brand], [product-name], buy online today.` at frontend of Magento then we can do.
    – Amit Bera
    Commented May 28, 2019 at 13:24
  • That case, you don't need to update meta description at the database level
    – Amit Bera
    Commented May 28, 2019 at 13:25
  • Hi @AmitBera yes it would be on the frontend, how would I achieve this? Commented May 28, 2019 at 15:08

2 Answers 2


In this case, you can use event/observer.

Fire an observer on the event catalog_product_load_after at frontend area set meta description on the fly.

Create events.xml at app/code/StackExchange/Magento/etc/frontend/ and as `events.xml location under on frontend and this event only fire for frontend area.

events.xml code

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="catalog_product_load_after">
        <observer instance="StackExchange\Magento\Observer\Frontend\Catalog\ProductLoadAfter" 

Create Observer class ProductLoadAfter at app/code/StackExchange/Magento/Observer/Frontend/Catalog/.

ProductLoadAfter.php code


namespace StackExchange\Magento\Observer\Frontend\Catalog;

class ProductLoadAfter implements \Magento\Framework\Event\ObserverInterface {

     * @var \Psr\Log\LoggerInterface
    private $logger;

    public function __construct(
     \Psr\Log\LoggerInterface $logger
    ) {

        $this->logger = $logger;
     * Execute observer
     * @param \Magento\Framework\Event\Observer $observer
     * @return void
    public function execute(
        \Magento\Framework\Event\Observer $observer
    ) {
        $product = $observer->getEvent()->getProduct();
        if ($product instanceof \Magento\Catalog\Model\Product) {
            $newMetaDescription = $product->getData('brand') .$product->getData('name').'-'. __('buy online today');
           // $product->setMetaKeyword('TEST '.$newMetaDescription);


Some notes:

Assume that brand is text type attribute. If the brand is an attribute of type drop attribute then you need to some extra code as $product->getData('brand') only give brand option id not it label mean brand name example, Puma, addidas.

Also, your module must have:

  1. app/code/{Vendor}/{Modulename}/etc/module.xml
  2. app/code/{Vendor}/{Modulename}/composer.json
  3. app/code/{Vendor}/{Modulename}/registration.php

After adding the event you should flush the cache.

  • Thanks Amit. Do I need a registration.php file for this module? Commented May 29, 2019 at 11:12
  • 1
    yes, it need..your module must have: app/code/{Vendor}/{Modulename}/etc/module.xml. app/code/{Vendor}/{Modulename}/composer.json . app/code/{Vendor}/{Modulename}/registration.php
    – Amit Bera
    Commented May 29, 2019 at 11:18
  • Hi Amit. Sorry about the late response on this... I tried it, and sadly, it's not having any effect on the front end. Did it work on your test site? Commented Jan 13, 2020 at 19:26
  • Hi @Amit Bera I've added code flushed cache etc. but this still doesn't produce a meta description on the front end. Commented Jan 21, 2020 at 10:24
  • Have u test the same at default Magento 2. Might some customization on your magento instance override this work
    – Amit Bera
    Commented Jan 21, 2020 at 10:52

Amit's answer is correct. However, Google limits the description to 255 characters. That is why I have limited the characters of the description to 255. I have also removed any new lines and html tags, if used in the description.

if ($product instanceof \Magento\Catalog\Model\Product) {
            $newMetaDescription = $product->getData('brand').' '. $product->getData('name') .'-'. __('buy online today');
            $shortMetaDescription = str_replace(array("\n", "\r"),' ',strip_tags(substr($newMetaDescription, 0, 255)));

I have also added the 'brand' attribute to the Meta Title and put the title in the Meta Keywords. Then put a comma after each word in the keywords. So if the new title is "New black tshirt Nike", the Meta Keywords, would be "new, black, tshirt, nike". Here is the code:

            $pageMainTitle = $product->getData('name');
            $newMetaTitle = $pageMainTitle .$product->getData('brand');
            $newKeywords = str_replace(' ',", ",$newMetaTitle);

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.