0

I'm trying to modify the "short_description" attribute using a plugin but I can't seem to get it to work across the whole website. I can do this on the product view page with the following but I don't know how to do this globally across the whole website.

I've been trying to create a plugin on "getAttributes" but nothing I try wants to work.

../app/code/TM/Customplugin/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\Block\Product\View">
        <plugin name="tm-block-plugin" type="TM\Customplugin\Plugin\GetProductPlugin" sortOrder="2" />
    </type>
</config>

../app/code/TM/Customplugin/Plugin/GetProductPlugin.php

<?php
    namespace TM\Customplugin\Plugin;
    class GetProductPlugin
    {    
        public function afterGetProduct(\Magento\Catalog\Block\Product\View $subject, $result)
        {
            $result['short_description'] = strtolower($result['short_description']);
            return $result;
        }
    }

UPDATE

This product attribute is being called through the helper class

$_helper->productAttribute($_product, $_product->getShortDescription(), 'short_description').

Using print_r(get_class_methods($_helper)) I get this:

Array ( [0] => __construct [1] => addHandler [2] => getHandlers [3] => process [4] => productAttribute [5] => categoryAttribute [6] => isDirectivesExists [7] => isModuleOutputEnabled )

Does anyone know if I need to create a plugin to intercept the productAttribute from the helper or is there another way?

SOLUTION

I ended up getting this to work using the preference class rewrite method and not the Plugin method. I know this isn't ideal but it works. If anyone knows how to achieve the same result using a Plugin please share :)

/app/code/TM/Module1/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">    
        <preference for="Magento\Catalog\Model\Product" type="TM\Module1\Model\Catalog\Product" />
    </config>

/app/code/TM/Module1/Model/Catalog/Product.php

<?php
namespace TM\Module1\Model\Catalog;
class Product extends \Magento\Catalog\Model\Product
{    
    public function getShortDescription()
    {        
        return strtolower($this['short_description']);
    }
}
?>
0

The plugin you are using is only valid for product detail page.

You can do it by adding plugin on class Magento\Catalog\Model\Product with method afterGetShortDescription().

Hope this helps you.

1
  • Hi Yash, I tried that but I still can't get it to work :( Here's what I have but it's not showing up in the log public function beforeGetShortDescription(\Magento\Catalog\Model\Product $subject, $result) { return strtolower($result); $logger = \Magento\Framework\App\ObjectManager::getInstance()->get('\Psr\Log\LoggerInterface'); $logger->debug('It is from DemoOverridePlugin '.__METHOD__ . ' line - ' . __LINE__); }
    – Johnny
    Feb 22 '20 at 3:24
0

1) Create this file in app/code/Solwin/Module1/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\Model\Product">
            <plugin name="sl-block-plugin" type="Solwin\Module1\Plugin\GetShortDescPlugin"/>
        </type>
    </config>

2) Create this file in app/code/Solwin/Module1/Plugin/GetShortDescPlugin.php

<?php
namespace Solwin\Module1\Plugin;
class GetShortDescPlugin
{
   public function afterGetShortDescription(Magento\Catalog\Model\Product $subject, $result)
   {
       $result['short_description'] = strtolower($result['short_description']);
            return $result;
    }  
}
?>
1
  • Sadly this doesn't work. As far as I can tell the GetShortDescription method isn't the default method used by Magento. In the template files it uses the helper class $_helper->productAttribute($_product, $_product->getShortDescription(), 'short_description') ...so I think I need to create a plugin that accesses productAttribute from within the $_helper class. I've Googled this for days and can't seem to find any answers :(
    – Johnny
    Feb 22 '20 at 12:06

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.