4

In magento 2, using Preference Configurable.php file is not simply override, Is this is magento 2 bug?

I have override simply using preference method inside di.xml file but its not override.

I want to override function getJsonConfig() from file. Using only Simply override method of magento 2. Code for di.xml file

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\ConfigurableProduct\Block\Product\View\Type\Configurable" type="Package\Configurable\Block\Product\View\Type\Configurable"/>    
</config>

Is there any trick or method to override Configurable.php file?

Thanks.

  • 1
    You can use plugin for this. – Sohel Rana Jun 22 '16 at 11:24
  • without plugin we cant do it? this is simple block file but not override, what is the issue behind this? – Rakesh Jesadiya Jun 22 '16 at 11:27
  • Whole file overwrite is not a good practice. I just add a plugin for you. – Sohel Rana Jun 22 '16 at 11:31
  • i have created using plugin but in listing page error are available – Rakesh Jesadiya Jun 22 '16 at 11:32
  • can you post answer for overrider getJsonConfig function using plugin, i want to add field qty inside config option for each simple product – Rakesh Jesadiya Jun 22 '16 at 11:32
6

VendorName/ModuleName/etc/frontend/di.xml


<?xml version="1.0"?>

    <type name="Magento\ConfigurableProduct\Block\Product\View\Type\Configurable">
        <plugin name="vendor_configurable_product_configurable" type="VendorName\ModuleName\Block\ConfigurableProduct\Product\View\Type\Configurable" sortOrder="1"/>
    </type>
</config>


namespace VendorName\Module\Block\ConfigurableProduct\Product\View\Type;

use Magento\Framework\Json\EncoderInterface;
use Magento\Framework\Json\DecoderInterface;
class Configurable
{

    protected $jsonEncoder;
    protected $jsonDecoder;

    public function __construct(
        EncoderInterface $jsonEncoder,
        DecoderInterface $jsonDecoder
    ) {

        $this->jsonDecoder = $jsonDecoder;
        $this->jsonEncoder = $jsonEncoder;
    }

    public function aroundGetJsonConfig(
        \Magento\ConfigurableProduct\Block\Product\View\Type\Configurable $subject,
        \Closure $proceed
    )
    {
        $config = $proceed();
        $config = $this->jsonDecoder->decode($config);
        $config['url'] = 'sample_url';
        return $this->jsonEncoder->encode($config);
    }
}

You can use after plugin following way:


namespace VendorName\Module\Block\ConfigurableProduct\Product\View\Type;

use Magento\Framework\Json\EncoderInterface;
use Magento\Framework\Json\DecoderInterface;
class Configurable
{

    protected $jsonEncoder;
    protected $jsonDecoder;

    public function __construct(
        EncoderInterface $jsonEncoder,
        DecoderInterface $jsonDecoder
    ) {

        $this->jsonDecoder = $jsonDecoder;
        $this->jsonEncoder = $jsonEncoder;
    }

    public function afterGetJsonConfig(
        \Magento\ConfigurableProduct\Block\Product\View\Type\Configurable $subject,
        $config
    )
    {
        $config = $this->jsonDecoder->decode($config);
        $config['url'] = 'sample_url';
        return $this->jsonEncoder->encode($config);
    }
}

  • Any reason why using the around instead of the after ? – Raphael at Digital Pianism Jun 22 '16 at 11:45
  • No reason, You can use after and around both plugin. – Sohel Rana Jun 22 '16 at 11:51
  • How to do above method using after plugin with same content in above function? – Rakesh Jesadiya Jun 22 '16 at 12:22
  • Its required in after plugin method to pass second parameter as $config? – Rakesh Jesadiya Jun 22 '16 at 13:27
  • @Rakesh The second parameter [$config] is the result of the original method call. – Keyur Shah Jun 22 '16 at 13:46

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.