How to override the vendor/magento/module-swatches/Block/Product/Renderer/Configurable.php in magento2 app/code/ custom module
-
12-05-2021 - |
Question
How to override the vendor/magento/module-swatches/Block/Product/Renderer/Configurable.php file in magento2 app/code/ custom module
I'm updated the const SWATCH_RENDERER_TEMPLATE = 'Magento_ConfigurableProduct::product/view/type/options/configurable.phtml';
please provide any suggestions
Thanks
No correct solution
OTHER TIPS
You can use preference property to overirde the file. Like below code:
Create file : [Vendor][Module]\etc\di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Acl/etc/acl.xsd">
<preference for="Magento\Swatches\Block\Product\Renderer\Configurable" type="[Vendor]\[Module]\Block\Rewrite\Product\Renderer\Configurable" />
</config>
Create class [Vendor][Module]\Block\Rewrite\Product\Renderer\Configurable to override Magento block
<?php
namespace [Vendor]\[Module]\Block\Rewrite\Product\Renderer;
class Configurable extends \Magento\Swatches\Block\Product\Renderer\Configurable
{
protected function getRendererTemplate()
{
return $this->isProductHasSwatchAttribute() ?
self::SWATCH_RENDERER_TEMPLATE : '[Vendor]_[Module]::product/view/type/options/configurable.phtml';
}
}
There are many ways to over-ride in magento 2.
Via Plugins, Observers and Events, Preferences
https://www.youtube.com/watch?v=ko75VJVor9I
Here is perfect video that will teach us all 3 ways to give override.