0

My product names are created by different values, that are stored in several custom modules. In my list.phtml I create the dynamic product names, that's no problem. But as I want, that the product names are available on all pages (search, list, checkout), I am looking for a possibility to create the product names already within the product collection. What I mean is, when the function getName() is called, the dynamic product name is given as result.

Thx, Christian

1 Answer 1

0

To create dynamic names of product, You can create plugin which will alter/modify original product name dynamically.

create a simple module say Pawan_SamplePlugin

app/code/Pawan/SamplePlugin/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Pawan_SamplePlugin',
    __DIR__
);

app/code/Pawan/SamplePlugin/etc/module.xml

    <?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="Pawan_SamplePlugin" setup_version="1.0.0" />
    </config>

app/code/Pawan/SamplePlugin/etc/di.xml

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Catalog\Model\Product">
      <plugin name="DynamicSetName" type="Pawan\SamplePlugin\Plugin\ProductName" disabled="false" />
    </type>
</config>

app/code/Pawan/SamplePlugin/Plugin/ProductName.php

<?php

namespace Pawan\SamplePlugin\Plugin;

class ProductName
{
    public function afterGetName(\Magento\Catalog\Model\Product $subject, $result)
    {
        return '|@@' . $result . '|##';
    }
}

You can Update afterGetName as per your requirement and update product name accordingly.

Hope above will help!

7
  • thanks a lot, I will try it and will give you a feedback Commented Aug 10, 2021 at 14:03
  • welcome :) @Dr.ChristianKusche, plz let me know if you have any issue.
    – Pawan
    Commented Aug 11, 2021 at 3:50
  • Great, it works! Is there a possibility tochoose the plugin depending on which siteis current. What I mean: Commented Aug 11, 2021 at 6:36
  • You can add Store dependency and can check current store/website and run above code accordingly.If above solution help/works for you, you can accept and upvote it :)
    – Pawan
    Commented Aug 11, 2021 at 7:06
  • Thanks, its working :-) Commented Aug 11, 2021 at 7:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.