8

How to override function getAllowProducts() from Magento\ConfigurableProduct\Block\Product\View\Type\Configurable.php in magento 2.

I want to override above function but not getting changes of function and nothing display any issue. There are no any error display inside log file.

i will share my code here,

registration.php file,

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Test_Configuration',
    __DIR__
);

etc folder,

module.xml 

code is,

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
  <module name="Test_Configuration" setup_version="1.0.0"/>
</config>

i have override block inside di.xml code is

<?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="Test\Configuration\Block\Outstock" />
</config>

Block folder, Outstock.php file,

<?php
namespace Test\Configuration\Block;

class Outstock extends \Magento\ConfigurableProduct\Block\Product\View\Type\Configurable
{
    public function getAllowProducts()
    {
        if (!$this->hasAllowProducts()) {
            $products = [];
            $skipSaleableCheck = $this->catalogProduct->getSkipSaleableCheck();
            $allProducts = $this->getProduct()->getTypeInstance()->getUsedProducts($this->getProduct(), null);
            foreach ($allProducts as $product) {
                    $products[] = $product;
            }
            $this->setAllowProducts($products);
        }
        return $this->getData('allow_products');
    }   

    protected function _toHtml()
    {
        $this->setModuleName($this->extractModuleName('Magento\ConfigurableProduct\Block\Product\View\Type\Configurable'));
        return parent::_toHtml();
    } 
}

i havent any error display not any log generate for this file and module is activated. inside setup_module entry are generated.

Any suggestion for override function of configurable.php file.

Thanks.

5

you don't have to override the preference and you should not.

You can easily use a Plugin to set the allow_products data before the method is called. You find a nice tutorial about plugins here:

http://alanstorm.com/magento_2_object_manager_plugin_system

To create your Plugin, you first need to add a type to your etc/frontend/di.xml

<type name="Magento\ConfigurableProduct\Block\Product\View\Type\Configurable">
    <plugin name="changeAllowProductsBehaviour" type="Vendor\Module\Model\ConfigurableProduct\Block\Product\View\Type\Configurable\Plugin" sortOrder="10" />
</type>

Then Your Plugin Class should look like this:

<?php
namespace Vendor\Module\Model\ConfigurableProduct\Block\Product\View\Type\Configurable;

class Plugin
{
    /**
     * getAllowProducts
     *
     * @param \Magento\ConfigurableProduct\Block\Product\View\Type\Configurable $subject
     *
     * @return array
     */
    public function beforeGetAllowProducts($subject)
    {
        if (!$subject->hasData('allow_products')) {
            $products = [];
            $allProducts = $subject->getProduct()->getTypeInstance()->getUsedProducts($subject->getProduct(), null);
            foreach ($allProducts as $product) {
                    $products[] = $product;
            }
            $subject->setData('allow_products', $products);
        }

        return [];
    }

}

Be sure to clear cache and also your var/generation dir to have this changes applied

  • its not working for me using plugin method. page disappear and only select box display with null value in whole page. – Rakesh Jesadiya Feb 13 '16 at 8:58
  • then there is most likely still an error somewhere. Check you error logs in magento and also your webserver error log. Do you have developer mode enabled in .htaccess file? Also I was not sure if you have to return an empty array or rather nothing when the orgininal method has no arguments (should not make a difference but could) – David Verholen Feb 13 '16 at 9:07
  • i have to display out of stock products option in dropdown so i have to remove if condition from above function but using plugin its not working also not display any error log.any suggestion. – Rakesh Jesadiya Feb 13 '16 at 9:19
  • Now that your'e asking this way, it may be sufficient if you just change the configuration to show out of stock products in frontend. In your backend go to Stores -> Configuration. Then Choose Catalog->Inventory Tab and change display out of stock products to yes – David Verholen Feb 13 '16 at 9:24
  • those settings i have did, i want to display out of stock product options inside config product. by default out of stock products config options not display inside dropdown of config product detail page. so i have to display those options in dropdown. – Rakesh Jesadiya Feb 13 '16 at 9:28
12

For Magento2.1 version you need to override Magento\Swatches\Block\Product\Renderer\Configurable

1) Create di.xml file in Folder Namespace\Module\etc

<?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\Swatches\Block\Product\Renderer\Configurable" type="Namespace\Module\Block\Outstock" />
</config>

2) Create Outstock.php Block file in Folder Namespace\Module\Block

<?php 

namespace Namespace\Module\Block;

class Outstock extends \Magento\Swatches\Block\Product\Renderer\Configurable
{

    public function getAllowProducts()
    {
        if (!$this->hasAllowProducts()) {
            $products = [];
            $skipSaleableCheck = $this->catalogProduct->getSkipSaleableCheck();
            $allProducts = $this->getProduct()->getTypeInstance()->getUsedProducts($this->getProduct(), null);
            foreach ($allProducts as $product) {
                if ($product->isSaleable() || $skipSaleableCheck) {
                    $products[] = $product;
                }
            }
            $this->setAllowProducts($products);
        }
        return $this->getData('allow_products');
    }


}
  • Should be type="Namespace\Module\Block\ Outstock" – Rooster242 Sep 11 '17 at 21:09
  • but it doesn't work for list page, Why ? – zed Blackbeard Jul 27 '18 at 15:11
3

You need to override

Magento\Swatches\Block\Product\Renderer\Configurable 

instead of overriding

Magento\ConfigurableProduct\Block\Product\View\Type\Configurable 

file.

  • but it doesn't work for list page, Why ? – zed Blackbeard Jul 27 '18 at 15:10
1

To override function of configurable.php file.

1) First create di.xml file in Folder Test/Configuration/etc

<?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="Test\Configuration\Block\Outstock" />
</config>

2) Now create Outstock.php Block file in Folder Test\Configuration\Block

<?php 

namespace Test\Configuration\Block;

use Magento\ConfigurableProduct\Block\Product\View\Type\Configurable;

class Outstock extends \Magento\ConfigurableProduct\Block\Product\View\Type\Configurable
{

    public function getAllowProducts()
    {
        if (!$this->hasAllowProducts()) {
            $products = [];
            $skipSaleableCheck = $this->catalogProduct->getSkipSaleableCheck();
            $allProducts = $this->getProduct()->getTypeInstance()->getUsedProducts($this->getProduct(), null);
            foreach ($allProducts as $product) {
                if ($product->isSaleable() || $skipSaleableCheck) {
                    $products[] = $product;
                }
            }
            $this->setAllowProducts($products);
        }
        return $this->getData('allow_products');
    }


}
  • but it doesn't work for list page, Why ? – zed Blackbeard Jul 27 '18 at 15:10
  • How to override \Magento\ConfigurableProduct\Block\Product\View\Type\Configurable i am trying but not working above code – Rv Singh Jan 10 at 12:27
  • In my requirement configurable product option drop down remove "Custom option" label – Rv Singh Jan 10 at 12:28

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.