Hello,
I would like to add the SKU code in Product Title so it displays as SKU Code - Product Title.
Of course the word SKU in it self have to be removed.
Any help would be appreciated.
Solved! Go to Solution.
Hello @mhegabmeco287a ,
If you want to change the product name only in the product detail page, please use below code:
Step 1: Create a frontend plugin in your custom module:
app/code/Vendor_Name/Module_name/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\Theme\Block\Html\Title"> <plugin name="product_title" type="Vendor_Name\Module_name\Plugin\ProductTitleWithSku" /> </type> </config>
Step 2: Create Plugin file as app/code/Vendor_Name/Module_Name/Plugin/ProductTitleWithSku.php and add the below code:
<?php
namespace Vendor_Name\Module_Name\Plugin;
use Magento\Framework\Registry; use Magento\Framework\App\Request\Http; class ProductTitleWithSku { /** * @var Registry */ private $coreRegistry; private static $attributePrefix = 'brand'; public function __construct( Registry $coreRegistry, Http $request ) { $this->coreRegistry = $coreRegistry; $this->request = $request; } public function afterGetPageHeading(\Magento\Theme\Block\Html\Title $subject, $title) { if ($this->request->getFullActionName() == 'catalog_product_view' && $this->getProduct()) { $prefix = $this->getProduct()->getSku(); if ($prefix) { $title = $prefix.'-'.$title; } } return $title; } /** * Retrieve currently viewed product object * * @return \Magento\Catalog\Model\Product */ private function getProduct() { return $this->coreRegistry->registry('product'); } }
If you want to change product name everywhere(in category page, checkout page etc.) use the following steps:
Step 1: Create a frontend plugin app/code/Vendor_Name/Module_Name/etc/frontend/di.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <type name="Magento\Catalog\Model\Product"> <plugin name="product_name_with_sku" type="Vendor_Name\Module_Name\Plugin\Catalog\Model\ProductTitleWithSku"/> </type> </config>
Step 2: Create a plugin file in app/code/Vendor_Name/Module_Name/Plugin/Catalod/Model/ProductTitleWithSku.php:
<?php
namespace Vendor_Name\Module_Name\Plugin\Catalog\Model;
class ProductTitleWithSku { public function afterGetName(\Magento\Catalog\Model\Product $subject, $result) { $title = $subject->getSku()." - ".$title; return $title; } }
Please refer https://devdocs.magento.com/videos/fundamentals/create-a-new-module/ this link to learn how to create a basic module.
Hope this will help you.
If this works for you please accept it as a solution and give kudos.
Step 1: create registration.php in app/code/Evo/ProductNameWithSku:
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Evo_ProductNameWithSku', __DIR__ );
Step 2: create module.xml in app/code/Evo/ProductNameWithSku/etc:
<?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="Evo_ProductNameWithSku" > <sequence> <module name="Magento_Catalog" /> </sequence> </module> </config>
Step 3: create di.xml in app/code/Evo/ProductNameWithSku/etc/frontend :
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <type name="Magento\Catalog\Model\Product"> <plugin name="product_name_with_sku" type="Evo\ProductNameWithSku\Plugin\Catalog\Model\ProductTitleWithSku"/> </type> </config>
Step 4: create ProductTitleWithSku.php in app/code/Evo/ProductNameWithSku/Plugin/Catalog/Model:
<?php namespace Evo\ProductNameWithSku\Plugin\Catalog\Model; class ProductTitleWithSku { public function afterGetName(\Magento\Catalog\Model\Product $subject, $result) { $title = $subject->getSku()." - ".$result; return $title; } }
Please try the above code. This will help you.
Hello @mhegabmeco287a ,
If you want to change the product name only in the product detail page, please use below code:
Step 1: Create a frontend plugin in your custom module:
app/code/Vendor_Name/Module_name/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\Theme\Block\Html\Title"> <plugin name="product_title" type="Vendor_Name\Module_name\Plugin\ProductTitleWithSku" /> </type> </config>
Step 2: Create Plugin file as app/code/Vendor_Name/Module_Name/Plugin/ProductTitleWithSku.php and add the below code:
<?php
namespace Vendor_Name\Module_Name\Plugin;
use Magento\Framework\Registry; use Magento\Framework\App\Request\Http; class ProductTitleWithSku { /** * @var Registry */ private $coreRegistry; private static $attributePrefix = 'brand'; public function __construct( Registry $coreRegistry, Http $request ) { $this->coreRegistry = $coreRegistry; $this->request = $request; } public function afterGetPageHeading(\Magento\Theme\Block\Html\Title $subject, $title) { if ($this->request->getFullActionName() == 'catalog_product_view' && $this->getProduct()) { $prefix = $this->getProduct()->getSku(); if ($prefix) { $title = $prefix.'-'.$title; } } return $title; } /** * Retrieve currently viewed product object * * @return \Magento\Catalog\Model\Product */ private function getProduct() { return $this->coreRegistry->registry('product'); } }
If you want to change product name everywhere(in category page, checkout page etc.) use the following steps:
Step 1: Create a frontend plugin app/code/Vendor_Name/Module_Name/etc/frontend/di.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <type name="Magento\Catalog\Model\Product"> <plugin name="product_name_with_sku" type="Vendor_Name\Module_Name\Plugin\Catalog\Model\ProductTitleWithSku"/> </type> </config>
Step 2: Create a plugin file in app/code/Vendor_Name/Module_Name/Plugin/Catalod/Model/ProductTitleWithSku.php:
<?php
namespace Vendor_Name\Module_Name\Plugin\Catalog\Model;
class ProductTitleWithSku { public function afterGetName(\Magento\Catalog\Model\Product $subject, $result) { $title = $subject->getSku()." - ".$title; return $title; } }
Please refer https://devdocs.magento.com/videos/fundamentals/create-a-new-module/ this link to learn how to create a basic module.
Hope this will help you.
If this works for you please accept it as a solution and give kudos.
I did the above but didn't work
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <type name="Magento\Catalog\Model\Product"> <plugin name="product_name_with_sku" type="evo\product_name_with_sku\Plugin\Catalog\Model\ProductTitleWithSku"/> </type> </config>
And this
<?php namespace evo\product_name_with_sku\Plugin\Catalog\Model; class ProductTitleWithSku { public function afterGetName(\Magento\Catalog\Model\Product $subject, $result) { $title = $subject->getSku()." - ".$title; return $title; } }
Step 1: create registration.php in app/code/Evo/ProductNameWithSku:
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Evo_ProductNameWithSku', __DIR__ );
Step 2: create module.xml in app/code/Evo/ProductNameWithSku/etc:
<?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="Evo_ProductNameWithSku" > <sequence> <module name="Magento_Catalog" /> </sequence> </module> </config>
Step 3: create di.xml in app/code/Evo/ProductNameWithSku/etc/frontend :
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <type name="Magento\Catalog\Model\Product"> <plugin name="product_name_with_sku" type="Evo\ProductNameWithSku\Plugin\Catalog\Model\ProductTitleWithSku"/> </type> </config>
Step 4: create ProductTitleWithSku.php in app/code/Evo/ProductNameWithSku/Plugin/Catalog/Model:
<?php namespace Evo\ProductNameWithSku\Plugin\Catalog\Model; class ProductTitleWithSku { public function afterGetName(\Magento\Catalog\Model\Product $subject, $result) { $title = $subject->getSku()." - ".$result; return $title; } }
Please try the above code. This will help you.
Amazing, Thanks a Million Man