0

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

2 Answers 2

2

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';
        }
    }
5
  • I tried this one but Not working @Amit Saini Commented May 11, 2021 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
    Commented May 11, 2021 at 12:57
  • added in Magento_Swatches module in etc/module.xml in sequence tag but still not working @Amit Saini Commented May 11, 2021 at 13:11
  • @venkataprasad Please share your complete code.
    – Amit Saini
    Commented May 11, 2021 at 14:48
  • it is working using plugin Commented May 12, 2021 at 11:41
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.