0

Use magento2.1 Need to generate meta title and description for my products dynamically, forexample: BASEURL-for your(Static string)-Product title, and for meta description example: static string-attribute1-attribute2

0

I just override

File: module-catalog/Block/Category/View.php

And add the below code. I am just providing you a logic. I have directly used a custom query. I have created a table to add URL, title, description, and keyword then I have matched current URL in the table and then set title, description, and keyword dramatically.

$urlInterface = \Magento\Framework\App\ObjectManager::getInstance()->get('Magento\Framework\UrlInterface');
        $current_url = $urlInterface->getCurrentUrl();

        if ($current_url)
        {
            $sqlQueryCustom_url_meta_data= "SELECT * FROM custom_url_meta_data where url='".$current_url."'";
            $resultCustom_url_meta_data=$connection->fetchAll($sqlQueryCustom_url_meta_data);
            if($resultCustom_url_meta_data)
            {
                $title = $resultCustom_url_meta_data[0]['meta_title'];
                $description = $resultCustom_url_meta_data[0]['meta_description'];
                $keywords = $resultCustom_url_meta_data[0]['meta_keywords'];
                $this->pageConfig->getTitle()->set($title);
                $this->pageConfig->setDescription($description);
                $this->pageConfig->setKeywords($keywords);
            }
        }

Hope it works!

| improve this answer | |
  • Thanks, could you please let me know how can i set sample static strings into this code? – MGPM Mar 18 at 9:26
  • you can directly set string to $this->pageConfig->getTitle()->set('title test'); $this->pageConfig->setDescription('description test'); $this->pageConfig->setKeywords('keyword test'); – Devidas Mar 18 at 14:28

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.