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



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" />

Create class [Vendor][Module]\Block\Rewrite\Product\Renderer\Configurable to override Magento block

    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';
  • I tried this one but Not working @Amit Saini May 11 at 12:53
  • Please add Magento_Swatches module in etc/module.xml in sequence tag for dependency and then run the upgrade and compile command.
    – Amit Saini
    May 11 at 12:57
  • added in Magento_Swatches module in etc/module.xml in sequence tag but still not working @Amit Saini May 11 at 13:11
  • @venkataprasad Please share your complete code.
    – Amit Saini
    May 11 at 14:48
  • it is working using plugin May 12 at 11:41

There are many ways to over-ride in magento 2.
Via Plugins, Observers and Events, Preferences
Here is perfect video that will teach us all 3 ways to give override.

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.