skip to Main Content

I am facing an issue with Magento2 and I am not sure whether it’s a bug or not. I want to show Out of Stock Assosiated Products in the Configurable Product Dropdown.

In the admin settings, Stores -> Configuration -> Catalog -> Inventory -> Display out of stock products, this is already set to “Yes” but no luck.

I have tried several modules with integrating plugins and did a lot of research on this but could not find any solution for this.

Lets take an example – I have a Configurable Product with Child products Size – S , M , L and for instance L is out of stock. So currently I am able to see S and M in the dropdown options for my configurable product.

I want to see all the 3 Child products with the mark of “Out of Stock” like L – Out of Stock or something like that.

2

Answers


  1. Chosen as BEST ANSWER

    sorry for the late reply. I was really busy with different projects.

    So for this, i used 1 module and did customized that based on my requirements. I am providing you all the files and folders so anyone who needs this, can upload the files.

    Created a module - Jworks/ConfigurableProduct so the folder structure will be /app/code/Jworks/ConfigurableProduct

    Create a file app/code/Jworks/ConfigurableProduct/registration.php and enter below code -

    <?php
    /**
     *
     * @category Jworks
     * @package ConfigurableProduct
     * @author Jitheesh V O <[email protected]>
     * @copyright Copyright (c) 2018 Digital Boutique (http://www.digitalboutique.co.uk/)
     */
    MagentoFrameworkComponentComponentRegistrar::register(
        MagentoFrameworkComponentComponentRegistrar::MODULE,
        'Jworks_ConfigurableProduct',
        __DIR__
    );
    

    Create a file app/code/Jworks/ConfigurableProduct/composer.json and enter below code -

    {
      "name": "jworks/module-configurableproduct",
      "description": "Jworks configurableproduct updates",
      "require": {
        "magento/module-catalog": "101.0.*"
      },
      "type": "magento2-module",
      "version": "1.0.0",
      "autoload": {
        "files": [
          "registration.php"
        ],
        "psr-4": {
          "Jworks\ConfigurableProduct\": ""
        }
      }
    }
    

    Create a file app/code/Jworks/ConfigurableProduct/etc/module.xml and enter below code -

    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
        <module name="Jworks_ConfigurableProduct" setup_version="0.0.1">
            <sequence>
                <module name="Magento_ConfigurableProduct" />
            </sequence>
        </module>
    </config>
    

    Create a file app/code/Jworks/ConfigurableProduct/etc/di.xml and enter below code -

    <?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="MagentoConfigurableProductModelConfigurableAttributeData" type="JworksConfigurableProductModelRewriteConfigurableAttributeData" />
    </config>
    

    Create a file app/code/Jworks/ConfigurableProduct/etc/frontend/di.xml and enter below code -

    <?xml version="1.0"?>
    <!--
    /**
     * Copyright © Magento, Inc. All rights reserved.
     * See COPYING.txt for license details.
     */
    -->
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
        <type name="MagentoConfigurableProductModelResourceModelAttributeOptionSelectBuilderInterface">
            <plugin name="Magento_ConfigurableProduct_Plugin_Model_ResourceModel_Attribute_InStockOptionSelectBuilder" type="JworksConfigurableProductPluginModelResourceModelAttributeInStockOptionSelectBuilder"/>
        </type>
    
        <type name="MagentoConfigurableProductModelAttributeOptionProvider">
            <plugin name="Magento_ConfigurableProduct_Plugin_Model_AttributeOptionProvider" type="JworksConfigurableProductPluginModelAttributeOptionProvider"/>
        </type>
    
        <type name="MagentoConfigurableProductBlockProductViewTypeConfigurable">
        <plugin name="changeAllowProductsBehaviour" type="JworksConfigurableProductModelConfigurableProductBlockProductViewTypeConfigurablePlugin" sortOrder="10" />
        </type>
    </config>
    

    Create a file app/code/Jworks/ConfigurableProduct/Plugin/Model/AttributeOptionProvider.php and enter below code -

    <?php
    
    
    namespace JworksConfigurableProductPluginModel;
    class AttributeOptionProvider
    {
    
        public function afterGetAttributeOptions(MagentoConfigurableProductModelAttributeOptionProvider $subject, array $result)
        {
            $optiondata=array();
            foreach ($result as $option) {  
    
                if(isset($option['stock_status']) && $option['stock_status']==0){
                    $option['option_title']  = $option['option_title'].__(' - uitverkocht');
                }
                $optiondata[]=$option;
            }
            return $optiondata;
        }
    }
    

    Create a file app/code/Jworks/ConfigurableProduct/Plugin/Model/ResourceModel/Attribute/InStockOptionSelectBuilder.php and enter below code -

    <?php
    
    namespace JworksConfigurableProductPluginModelResourceModelAttribute;
    
    use MagentoCatalogInventoryModelResourceModelStockStatus;
    use MagentoConfigurableProductModelResourceModelAttributeOptionSelectBuilderInterface;
    use MagentoFrameworkDBSelect;
    
    /**
     * Plugin for OptionSelectBuilderInterface to add stock status filter.
     */
    class InStockOptionSelectBuilder
    {
        /**
         * CatalogInventory Stock Status Resource Model.
         *
         * @var Status
         */
        private $stockStatusResource;
    
        /**
         * @param Status $stockStatusResource
         */
        public function __construct(Status $stockStatusResource)
        {
            $this->stockStatusResource = $stockStatusResource;
        }
    
        /**
         * Add stock status filter to select.
         *
         * @param OptionSelectBuilderInterface $subject
         * @param Select $select
         * @return Select
         *
         * @SuppressWarnings(PHPMD.UnusedFormalParameter)
         */
        public function afterGetSelect(OptionSelectBuilderInterface $subject, Select $select)
        {
            $select->joinInner(
                ['stock' => $this->stockStatusResource->getMainTable()],
                'stock.product_id = entity.entity_id',
                ['stock.stock_status']
            );
    
            return $select;
        }
    }
    

    Create a file app/code/Jworks/ConfigurableProduct/Model/Rewrite/ConfigurableAttributeData.php and enter below code -

    <?php
    /**
     * Copyright © Magento, Inc. All rights reserved.
     * See COPYING.txt for license details.
     */
    
    namespace MagentoConfigurableProductModel;
    
    use MagentoCatalogModelProduct;
    use MagentoConfigurableProductModelProductTypeConfigurableAttribute;
    
    /**
     * Class ConfigurableAttributeData
     * @api
     * @since 100.0.2
     */
    class ConfigurableAttributeData
    {
        /**
         * Get product attributes
         *
         * @param Product $product
         * @param array $options
         * @return array
         */
        public function getAttributesData(Product $product, array $options = [])
        {
            $defaultValues = [];
            $attributes = [];
            foreach ($product->getTypeInstance()->getConfigurableAttributes($product) as $attribute) {
                $attributeOptionsData = $this->getAttributeOptionsData($attribute, $options);
                if ($attributeOptionsData) {
                    $productAttribute = $attribute->getProductAttribute();
                    $attributeId = $productAttribute->getId();
                    $attributes[$attributeId] = [
                        'id' => $attributeId,
                        'code' => $productAttribute->getAttributeCode(),
                        'label' => $productAttribute->getStoreLabel($product->getStoreId()),
                        'options' => $attributeOptionsData,
                        'position' => $attribute->getPosition(),
                    ];
                    $defaultValues[$attributeId] = $this->getAttributeConfigValue($attributeId, $product);
                }
            }
            return [
                'attributes' => $attributes,
                'defaultValues' => $defaultValues,
            ];
        }
    
        /**
         * @param Attribute $attribute
         * @param array $config
         * @return array
         */
        protected function getAttributeOptionsData($attribute, $config)
        {
            $attributeOptionsData = [];
            foreach ($attribute->getOptions() as $attributeOption) {
                $optionId = $attributeOption['value_index'];
                $attributeOptionsData[] = [
                    'id' => $optionId,
                    'label' => $attributeOption['label'],
                    //'test' => $config[$attribute->getAttributeId()][$optionId],
                    'products' => isset($config[$attribute->getAttributeId()][$optionId])
                        ? $config[$attribute->getAttributeId()][$optionId]
                        : [],
                ];
            }
            return $attributeOptionsData;
        }
    
        /**
         * @param int $attributeId
         * @param Product $product
         * @return mixed|null
         */
        protected function getAttributeConfigValue($attributeId, $product)
        {
            return $product->hasPreconfiguredValues()
                ? $product->getPreconfiguredValues()->getData('super_attribute/' . $attributeId)
                : null;
        }
    }
    

    Create a file app/code/Jworks/ConfigurableProduct/Model/ConfigurableProduct/Block/Product/View/Type/Configurable/Plugin.php and enter below code -

    <?php
    namespace JworksConfigurableProductModelConfigurableProductBlockProductViewTypeConfigurable;
    
    class Plugin
    {
        /**
         * getAllowProducts
         *
         * @param MagentoConfigurableProductBlockProductViewTypeConfigurable $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 [];
        }
    
    }
    

    And after all these, you can run all the commands for installing the Magento2 extension.

    I hope anybody can get help with this.


  2. open the file vendormagentomodule-catalogHelperProduct.php

    change

    protected $_skipSaleableCheck = false;
    

    to

    protected $_skipSaleableCheck = true;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search