2

we're trying to dynamically add the SKU to the page's tag in each product page's .

Our approach so far has been to override the Magento\Catalog\Block\Product\View class:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
    <preference for="Magento\Catalog\Block\Product\View" type="Brock\ProductTitles\Block\Product\View" />
</config>

And then in the module:

class View extends \Magento\Catalog\Block\Product\View{

  public function _prepareLayout()
  {

      $product = $this->getProduct();
      if (!$product) {
          return parent::_prepareLayout();
      }
      $title = $product->getMetaTitle();
      $sku = $product->getSku();
      if ($title) {
        echo $title;
        echo $sku;
          $this->pageConfig->getTitle()->set($title." - ".$sku);
      }
  }

}

Any ideas why this wouldn't be working? Or better/different ways of achieving this?

Worth nothing that when we make the above changes to the _prepareLayout function in the core file, it works, just having issues with the override.

0

There is another way, you can changes the page title without rewrite the class.

Using layout_generate_blocks_before event, you can changes page title.

Fire an observer on this event which will change -getBlock('page.main.title')'s setPageTitle value.

<?php
namespace Devamitbera\ChangePagetitle\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\Framework\App\ObjectManager;

class ObserverChangePagetitleForProduct implements ObserverInterface
{



    protected $_pageConfig;
    protected $_registry;   

    public function __construct(
        \Magento\Framework\View\Page\Config $pageConfig  ,
     \Magento\Framework\Registry $registry   
    ) {

        $this->_pageConfig = $pageConfig; 
    }

    /**
     *
     * @param \Magento\Framework\Event\Observer $observer
     * @return void
     */
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        /** @var \Magento\Framework\App\Action\Action $controller */
        $full_action_name = $observer->getFullActionName();

        $layout = $observer->getEvent()->getLayout();
    if($full_action_name != 'catalog_product_view'){
    return $this;
    }
    $product = $this->registry->registry('current_product')
        if($product){
        $title = $product->getMetaTitle();
        $sku = $product->getSku();
             $layout->getBlock('page.title')->setPageTitle($title." - ".$sku);
        }
       return $this;    
    }

}
4
  • This set us on the right track, thank you. We had to add calls to some IsMetaApplied methods in our code in order to get it to work for some reason, but not sure why.
    – brackfost
    Aug 23 '18 at 16:04
  • 2
    Hello, I got below error while using above code. Fatal error: Uncaught Error: Call to a member function setPageTitle() on boolean can you please help someone?
    – Pribhav
    Jan 8 '19 at 6:48
  • @Pribhav Same issue for me !! in Mangento 2.3 Feb 21 '19 at 5:59
  • Error: Call to a member function setPageTitle() on bool, getting this error in magento-2.4 Jun 2 at 7:23
2
class View extends \Magento\Catalog\Block\Product\View{

  public function _prepareLayout()
  {

      $product = $this->getProduct();
      if (!$product) {
          return parent::_prepareLayout();
      }
      $title = $product->getMetaTitle();
      $sku = $product->getSku();
      if ($title) {
        echo $title;
        echo $sku;
          $this->pageConfig->getTitle()->set($title." - ".$sku);
          return parent::_prepareLayout();
      }
  }
}

Regarless you have to call parent::_prepareLayout();

0

The code provided by Amit is not correct. Here the correct version:

    <?php

namespace Devamitbera\ChangePagetitle\Observer;

use Magento\Framework\Event\ObserverInterface;

use \Magento\Framework\Registry;

class ObserverChangePageTitleForProduct implements ObserverInterface
{


    private $registry;

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

    /**
     *
     * @param \Magento\Framework\Event\Observer $observer
     * @return void
     */
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        /** @var \Magento\Framework\App\Action\Action $controller */
        $full_action_name = $observer->getFullActionName();

        $layout = $observer->getEvent()->getLayout();
        if ($full_action_name != 'catalog_product_view') {
            return;
        }
        $product = $this->registry->registry('current_product');
        if ($product) {
            $title = $product->getMetaTitle();
            $sku = $product->getSku();
            $layout->getBlock('page.title')->setPageTitle($title . " - " . $sku);
        }
       return;
    }

}
1
  • How to set the same page title for all pages such app "Application Name". I tried but page goes to loading and loading. May 8 '20 at 10:45

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.