Question

I want to add the SKU beside the product name in "product edit page" at the admin-side in magneto 2? i have changed in Magento\Catalog\Block\Adminhtml\Product\Edit.php here

public function getHeader()
{
    if ($this->getProduct()->getId()) {
        $header = $this->escapeHtml($this->getProduct()->getName().'#sku'.$this->getProduct()->getSku());
    } else {
        $header = __('New Product');
    }
    return $header;
}

but it is not working in magneto 2.3

Was it helpful?

Solution

The page title has been set in the controller file for the admin product edit page. You can update the product name using a plugin of the controller function.

Create the following files in your module to update the product name.

app/code/Vendor/Module/etc/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\Controller\Adminhtml\Product\Edit">
        <plugin name="add_sku_after_product_name" type="Vendor\Module\Plugin\Adminhtml\Catalog\Product\Edit"/>
    </type>

</config>

app/code/Vendor/Module/Plugin/Adminhtml/Catalog/Product/Edit.php

<?php

namespace Vendor\Module\Plugin\Adminhtml\Catalog\Product;

class Edit
{

    /**
     * @var \Magento\Framework\Registry
     */
    protected $registry;

    /**
     * @var \Magento\Framework\View\Result\PageFactory
     */
    protected $resultPageFactory;

    /**
     * @param \Magento\Framework\Registry $registry
     * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
     */
    public function __construct(
        \Magento\Framework\Registry $registry,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory
    ) {
        $this->registry = $registry;
        $this->resultPageFactory = $resultPageFactory;
    }

    public function afterExecute(
        \Magento\Catalog\Controller\Adminhtml\Product\Edit $subject,
        $result
    ) {
        $resultPage = $this->resultPageFactory->create();
        $product = $this->registry->registry('current_product');
        $resultPage->getConfig()->getTitle()->prepend($product->getName() . ' (' . $product->getSku() . ')');
        return $result;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top