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; }
-
The function getRendererTemplate runs into _toHtml() how a parameter of setTemplate().JuanP Barba– JuanP Barba2016年01月23日 12:05:17 +00:00Commented Jan 23, 2016 at 12:05
2 Answers 2
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
-
Can you specify how to do it? When I do beforeSetTemplate(){...} I'm overwriting the template of the page and not of the output of _toHtml() function...JuanP Barba– JuanP Barba2016年01月23日 12:01:24 +00:00Commented Jan 23, 2016 at 12:01
-
I add details to answer. _toHtml() call setTemplateKAndy– KAndy2016年01月23日 12:15:08 +00:00Commented Jan 23, 2016 at 12:15
An alternative is to override it using dependency injection.
Modify di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../vendor/magento/framework/ObjectManager/etc/config.xsd">
<preference for="Magento\Swatches\Block\Product\Renderer\Configurable" type="vendorName\moduleName\Block\Rewrite\Product\Renderer\Configurable" />
</config>
Create app/code/vendorName/moduleName/Block/Rewrite/Product/Renderer/Configurable.php
namespace vendorName\moduleName\Block\Rewrite\Product\Renderer;
class Configurable extends \Magento\Swatches\Block\Product\Renderer\Configurable {
protected function getRendererTemplate() {
return $this->isProductHasSwatchAttribute ?
self::SWATCH_RENDERER_TEMPLATE : 'vendorName_moduleName::product/view/type/options/configurable.phtml';
}
}
Create app/code/vendorName/moduleName/product/view/type/options/configurable.phtml to override the template.