1

I overrided the Magento\Catalog\Block\Product\View.php adding this code

protected function _prepareLayout()
{
    parent::_prepareLayout();
    $this->pageConfig->getTitle()->set($title . '- suffix');

    return $this;
}

And added the <preference for="Magento\Catalog\Block\Product\View" type="Indetach\Catalog\Block\Product\View" />

But it's not working....

  • run di:compil command – fmsthird May 25 at 12:27
  • I did it, but the seo title is still the same on the product page... – xdev May 25 at 13:18
0

You may try to use plug in for Magento\Catalog\Helper\Product\View as describe below to override Product View Page SEO title.

I assume you are using a custom module named "Company_MyModule".

step 1:

Create di.xml under YOUR-MAGENTO-ROOT/app/code/Company/MyModule/etc/frontend/

File : YOUR-MAGENTO-ROOT/app/code/Company/MyModule/etc/frontend/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Catalog\Helper\Product\View">
        <plugin name="CompanyModuleProductViewHelperData" type="Company\MyModule\Plugin\ProductPlugin" sortOrder="10"/>
    </type>  
</config>

step 2:

Create ProductPlugin.php under YOUR-MAGENTO-ROOT/app/code/Company/MyModule/Plugin/

File : YOUR-MAGENTO-ROOT/app/code/Company/MyModule/Plugin/ProductPlugin.php

<?php

namespace Company\MyModule\Plugin;

use Magento\Catalog\Helper\Product\View as ProductViewHelper;
use Magento\Catalog\Model\Product;
use Magento\Framework\DataObject;
use Magento\Framework\View\Result\Page as ResultPage;
use Magento\Framework\View\Result\Page;

class ProductPlugin
{


        public function beforeInitProductLayout(
        ProductViewHelper $subject,
        $resultPage,
        $product,
        $params
    ) {


        if ($resultPage instanceof ResultPage  ) {
             $resultPage->getConfig()->getTitle()->set('YOUR PRODUCT SEO TITLE');
        }
        return [
            $resultPage,
            $product,
            $params
        ];
    }  


}

Step 3: Run DI compile

php bin/magento setup:di:compile

enter image description here

  • I tried it out, the product still appears, but the seo title and the page title now is "404 error", the method to override is "preparePageMetadata" but there's no way to do that cause it's private.... – xdev May 25 at 22:52
  • Please try to override initProductLayout ( not preparePageMetadata) of the class Magento\Catalog\Helper\Product\View (as per my answer). I successfully override SEO Title by using before Plugin for public method initProductLayout. Thank you – Pritam Info 24 May 26 at 10:05
  • Is your magento version 2.3? I can't override it, I did it but when I get to the product page, the product is going to load but the seo title and the page title show 404 error – xdev May 26 at 10:28
  • No. My Magento version is not 2.3 but I can test the code in M 2.3 and let to know. – Pritam Info 24 May 26 at 10:35
  • My bad, it doesn't show the 404 error but the seo title still not changing... – xdev May 26 at 11:11

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.