1

I want to Change my header title tag product wise dynamically.my Magento version is 2.3.4. how can I get SKU title to set header title tag? I want to solve this problem. I want to Dynamically change title

1 Answer 1

1

Create a custom module.

app/code/Cavendish/ProductPageTitle/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Cavendish_ProductPageTitle',
    __DIR__
);

app/code/Cavendish/ProductPageTitle/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Cavendish_ProductPageTitle" setup_version="1.0.0" />
</config>

app/code/Cavendish/ProductPageTitle/etc/frontend/events.xml

<?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="layout_generate_blocks_before">
      <observer name="product_page_title_change" instance="Cavendish\ProductPageTitle\Observer\ChangeProductPageTitle"/>
   </event>
</config>

app/code/Cavendish/ProductPageTitle/Observer/ChangeProductPageTitle.php

<?php

namespace Cavendish\ProductPageTitle\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Exception\CouldNotSaveException;

class ChangeProductPageTitle implements ObserverInterface
{
    protected $registry;

    public function __construct(
    \Magento\Framework\Registry $registry   
    ) {
        $this->registry = $registry; 
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $full_action_name = $observer->getFullActionName();

        if($full_action_name != 'catalog_product_view'){
            return $this;
        }
        $product = $this->registry->registry('current_product');
        if($product){
            $title = $product->getMetaTitle();
            $sku = $product->getSku();
            $product->setMetaTitle($title." - ".$sku);// $product->setMetaTitle('Your Title For Product Page');
        }
       return $this;   
    }
}
6
  • There is some issue :- 1 exception(s): Exception #0 (ReflectionException): Class [Vendor][Module]\Observer\ChangeProductPageTitle does not exist Commented Jan 21, 2021 at 7:13
  • It's mean not clear @klaus :( what does mean 230 character in body?? Commented Jan 21, 2021 at 7:18
  • Share your files and code here. Commented Jan 21, 2021 at 13:26
  • Checkout This link :- imgur.com/a/GwCzpRa last image dicomple error Commented Jan 22, 2021 at 7:22
  • You need to replace "[Vendor]" and "[Module]" words with your chosen name. Anyways check out the updated answer. Commented Jan 23, 2021 at 13:04

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.