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']);
}
}
?>