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
1 Answer
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!
-
Thanks, could you please let me know how can i set sample static strings into this code?– MGPMCommented Mar 18, 2020 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');– DevidasCommented Mar 18, 2020 at 14:28