2
votes

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
This is a known issue that requires a code fix rather than change of settings - github.com/magento/magento2/issues/10454Andrew
Yes, i gone through this link but this is related to the swatches. And i dont have Swatches, i have a configurable dropdown with child products, and i was in need to out of stock label inside the dropdown for child products. But good news is that i have figured out the solution, and will post my answer shortly, if anyone need this requirement.Hardik
@hardik please shareRam Pratap Singh Gour
I am also tryin with select dropdownRam Pratap Singh Gour

2 Answers

1
votes

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/)
 */
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::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="Magento\ConfigurableProduct\Model\ConfigurableAttributeData" type="Jworks\ConfigurableProduct\Model\Rewrite\ConfigurableAttributeData" />
</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="Magento\ConfigurableProduct\Model\ResourceModel\Attribute\OptionSelectBuilderInterface">
        <plugin name="Magento_ConfigurableProduct_Plugin_Model_ResourceModel_Attribute_InStockOptionSelectBuilder" type="Jworks\ConfigurableProduct\Plugin\Model\ResourceModel\Attribute\InStockOptionSelectBuilder"/>
    </type>

    <type name="Magento\ConfigurableProduct\Model\AttributeOptionProvider">
        <plugin name="Magento_ConfigurableProduct_Plugin_Model_AttributeOptionProvider" type="Jworks\ConfigurableProduct\Plugin\Model\AttributeOptionProvider"/>
    </type>

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

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

<?php


namespace Jworks\ConfigurableProduct\Plugin\Model;
class AttributeOptionProvider
{

    public function afterGetAttributeOptions(\Magento\ConfigurableProduct\Model\AttributeOptionProvider $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 Jworks\ConfigurableProduct\Plugin\Model\ResourceModel\Attribute;

use Magento\CatalogInventory\Model\ResourceModel\Stock\Status;
use Magento\ConfigurableProduct\Model\ResourceModel\Attribute\OptionSelectBuilderInterface;
use Magento\Framework\DB\Select;

/**
 * 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 Magento\ConfigurableProduct\Model;

use Magento\Catalog\Model\Product;
use Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute;

/**
 * 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 Jworks\ConfigurableProduct\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 [];
    }

}

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

I hope anybody can get help with this.

0
votes

open the file vendor\magento\module-catalog\Helper\Product.php

change

protected $_skipSaleableCheck = false;

to

protected $_skipSaleableCheck = true;