Magento – Update Const template at Magento 2 Class

classmagento2plugintemplate

I need to update a template defined in a Constant at Configurable Class(Magento/Swatches/Block/Product/Renderer/Configurable.php).

I tried with a Plugin, but how the function which gets the constant is protected, I can't update the Constant correctly.

Anyone knows the correct way to update this constant or rewrite the function to change the path to my Module template.

const CONFIGURABLE_RENDERER_TEMPLATE = 'Magento_ConfigurableProduct::product/view/type/options/configurable.phtml';

protected function getRendererTemplate()
{
return $this->isProductHasSwatchAttribute ?
self::SWATCH_RENDERER_TEMPLATE :
self::CONFIGURABLE_RENDERER_TEMPLATE;
}

Best Answer

You can create before plugin on setTemplate method and overwrite template argument.

Create plugin

class ProductSwatchPlugin
{
    public function beforeSetTemplate(
        \Magento\Swatches\Block\Product\Renderer\Configurable $subject,
        $template
    ) {
         return ['You_Module::template.phtml'];
    }
}

and declare it in DI.xml

<type name="\Magento\Swatches\Block\Product\Renderer\Configurable">
    <plugin name="you_module_change_template" type="ProductSwatchPlugin" />
</type>

See more details in official documentation