I have a product called "Wiper blades 60/50cm" which is listed in the store Category "BMW 3 Series e46" I want to add the Category name to the product meta title so that the meta title becomes "Wiper blades 60/50cm BMW 3 Series e46".
I know how to add the Category name to the product name on the frontend page. I don't know how to do it in the meta title. I also know how to add a custom attribute to the meta title. I do it like this in my custom extension:
namespace Tnt\MakeModelInMeta\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();
$category = $observer->getEvent()->getCategory();
if ($product instanceof \Magento\Catalog\Model\Product) {
$pageMainTitle = $product->getData('name');
$newMetaTitle = $pageMainTitle .$product->getData('make_model');
$product->setMetaTitle($newMetaTitle);
}
}
}
Please help me add the category name in the way that the attribute "make_model" was added in the meta title.