2

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. – Dhaval Solanki May 28 '19 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 May 28 '19 at 13:24
  • That case, you don't need to update meta description at the database level – Amit Bera May 28 '19 at 13:25
  • Hi @AmitBera yes it would be on the frontend, how would I achieve this? – YorkieMagento May 28 '19 at 15:08
3

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" 
                  name="stackexchange_magento_observer_frontend_catalog_productloadafter_catalog_product_load_after"
        />
    </event>
</config>

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

ProductLoadAfter.php code

<?php

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
    ) {
        $this->logger->debug(__METHOD__);
        $product = $observer->getEvent()->getProduct();
        if ($product instanceof \Magento\Catalog\Model\Product) {
            $newMetaDescription = $product->getData('brand') .$product->getData('name').'-'. __('buy online today');
            $product->setMetaDescription($newMetaDescription);
           // $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.

| improve this answer | |
  • Thanks Amit. Do I need a registration.php file for this module? – YorkieMagento May 29 '19 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 May 29 '19 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? – YorkieMagento Jan 13 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. – YorkieMagento Jan 21 at 10:24
  • Have u test the same at default Magento 2. Might some customization on your magento instance override this work – Amit Bera Jan 21 at 10:52
1

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)));
            $product->setMetaDescription($shortMetaDescription);

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');
            $product->setMetaTitle($newMetaTitle);
            $newKeywords = str_replace(' ',", ",$newMetaTitle);
            $product->setMetaKeyword($newKeywords);
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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