Adding a productAttribute handler in Magento 2.4
-
15-04-2021 - |
سؤال
I'd like my Magento 2.4 module to be able to modify the output of one of the product attributes programatically. I see that the ability to create a handler for this purpose was added in Magento 2.3.4, using di.xml, however, I'm unclear on how to actually set this up. What do I need to do to make a simple productAttribute handler that modifies the output for one of my attributes?
المحلول
A developer posted the answer here:
etc/di.xml:
<type name="Magento\Catalog\Helper\Output">
<arguments>
<argument name="handlers" xsi:type="array">
<item name="productattribute" xsi:type="array">
<item name="myHandler" xsi:type="object">Vendor\Module\Model\Handler\Output</item>
</item>
<item name="categoryattribute" xsi:type="array">
<item name="myHandler" xsi:type="object">Vendor\Module\Model\Handler\Output</item>
</item>
</argument>
</arguments>
Vendor\Module\Model\Handler\Output.php:
namespace Vendor\Module\Model\Handler;
use Magento\Catalog\Helper\Output as HelperOutput;
class Output
{
public function productAttribute(HelperOutput $output, string $attributeHtml, array $params): string
{
// $params: ['product' => $product, 'attribute' => $attributeName]
// todo your output handler
return $attributeHtml;
}
public function categoryAttribute(HelperOutput $output, string $attributeHtml, array $params): string
{
// $params: ['category' => $category, 'attribute' => $attributeName]
// todo your output handler
return $attributeHtml;
}
}
لا تنتمي إلى magento.stackexchange