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.

8
  • 1
    You can use plugin for this.
    – Sohel Rana
    Jun 22, 2016 at 11:24
  • without plugin we cant do it? this is simple block file but not override, what is the issue behind this? Jun 22, 2016 at 11:27
  • Whole file overwrite is not a good practice. I just add a plugin for you.
    – Sohel Rana
    Jun 22, 2016 at 11:31
  • i have created using plugin but in listing page error are available Jun 22, 2016 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 Jun 22, 2016 at 11:32

1 Answer 1

6

VendorName/ModuleName/etc/frontend/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <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>

<?php

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:

<?php

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

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

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.