1

how I can change products name attribute. I create a new attribute second_name and I assigned to all products, now I want this attribute to replace the default name attribute. I need to change this on products view page.

I try to add something like this but is not working:

   namespace Company\Module\Helper;

    class Data extends \Magento\Framework\App\Helper\AbstractHelper
    {
        protected $registry;

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

        public function getTitle()
        {
            $this->product = $this->registry->registry('product');
            $title = $this->product->getAttributeText('second_name');
            return $title;
        }
    } 

and I add this code in catalog_product_view.xml

<referenceBlock name="page.main.title">
    <action method="setPageTitle">
        <argument name="title" xsi:type="helper" helper="Company\Module\Helper\Data::getTitle"></argument>
    </action>
</referenceBlock>

So in product view page instead Product Name I need to be my custom attribute.

Thanks

4

I can suggest solving your problem. You should create a custom module and then use Plugin as following steps:

  1. app/code/Company/Module/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="training_catalog_product" type="Company\Module\Plugin\Catalog\Model\Product"/>
    </type>
</config>
  1. app/code/Company/Module/Plugin/Catalog/Model/Product.php
class Product
{
    public function afterGetName(\Magento\Catalog\Model\Product $subject, $result)
    {
        $title = $subject->getAttributeText('second_name');
        return $title;
    }
}

You can try these steps and let me know your result.

  • How can we do without plugin ? – Manish Maheshwari Feb 21 at 6:39
  • I don't know why you would want to do this without a plugin. This is very straightforward and works perfectly! – seanbreeden May 10 at 14:57

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.