2

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; }

asked Jan 23, 2016 at 11:02
1
  • The function getRendererTemplate runs into _toHtml() how a parameter of setTemplate(). Commented Jan 23, 2016 at 12:05

2 Answers 2

7

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

answered Jan 23, 2016 at 11:35
2
  • 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... Commented Jan 23, 2016 at 12:01
  • I add details to answer. _toHtml() call setTemplate Commented Jan 23, 2016 at 12:15
6

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.

answered Aug 7, 2016 at 7:53
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.