3

The display of out of stock products is enabled in the store configuration. This means that out of stock simple products are displayed. But if one or more configuration of a configurable product is out of stock then the swatch of that product is missing from the swatch list of that product.

How do I make all swatches visible, even for out of stock configurations?

There have been a number of questions and answers in this topic but none of them work. Particularly the one that recommends to set Use configuration options under the products' Advanced inventory. This option is not even there in M2.2.7. Another solution involves a custom module but according to comments some files are missing.

The solution posted here is no help either because stock is refreshed from our remote server via Magento 2's API multiple times a day, so setting a stock manually for these simple products would be pointless.

So please do not mark this as a duplicate.

Thanks in advance for all the help!

3 Answers 3

3

For anyone else coming here for a solution, I ended up creating a module for this, which is available from here: https://github.com/pkarsai/show-out-of-stock-products-magento2

The problem is – and that's why the solutions available on the web were not working – that modifying getAllowProducts() alone – as Amit suggested – is not enough. Because there is a second check in Magento code, in the Magento\ConfigurableProduct\Plugin\Model\ResourceModel\AttributeInStockOptionSelectBuilder where all entries that are not on stock are removed:

public function afterGetSelect(OptionSelectBuilderInterface $subject, Select $select)
{
    $select->joinInner(
        ['stock' => $this->stockStatusResource->getMainTable()],
        'stock.product_id = entity.entity_id',
        []
    )->where(
        'stock.stock_status = ?',
        \Magento\CatalogInventory\Model\Stock\Status::STATUS_IN_STOCK
    );

    return $select;
}

So we also need to create a plugin that skips this code. Furthermore, if we stop here then we get the swatches for the products that are not on stock, but nothing indicates that they are out-of-stock until the customer tries to put them into the shopping-basket. So we also need code to that displays a crossed-out swatch when the particular configuration is not available.

TL;DR I won't get into many details here because it's a complex issue, but check out my code here for the complete solution: https://github.com/pkarsai/show-out-of-stock-products-magento2

6
  • 1
    Excellent! I've been looking all over for this solution and this is the only one that's worked. Thank you so much. Apr 11, 2019 at 10:07
  • I'm glad that it helped Apr 12, 2019 at 11:16
  • 1
    Note this solution does not work when using Magento 2 with the MSI inventory module, which is enabled by default from version 2.3.0. It results in all swatches being disabled, since getStockItem is no longer used to get the quantity. One of my colleagues is working on modifying this module to support MSI. I will ask him to post the required changes here after he is done.
    – micwallace
    Feb 5, 2020 at 6:51
  • Hi, I'm getting an issue with this plugin and Magento 2.3.4. After install I get a "Page not found" error, logs shows "main.CRITICAL: Type Error occurred when creating object: Myweb\ShowOutOfStockProducts\Plugin\InStockOptionSelectorPlugin". Looks like StockConfigurationInterface is deprecated. I had a look at @bryanxkier enhancement but it's not better on my side. Any idea please?
    – David
    Mar 24, 2020 at 10:19
  • Erasing InStockOptionSelectorPlugin content got me rid of the error but my out of stock children do not show.
    – David
    Mar 24, 2020 at 10:58
0

You can try the below code :

Go to -> vendor\magento\module-configurable-product\Block\Product\View\Type\Configurable.php

Go to function getAllowProducts()

Replace the code from :

if ($product->isSaleable() || $skipSaleableCheck) {
  $products[] = $product;
}

To :

$products[] = $product;

Note : Do not make the direct changes in the core file try to override this function in your module.

1
  • Thanks for your input, but it ended up being much more complicated than that. See my answer for details. Apr 3, 2019 at 17:19
0

As @micwallace mentioned in a comment, this is no longer working properly in Magento 2.3.0 and later specially when MSI inventory module is enabled. Thus, we need to modify the getStockItem()

$stockitem = $stockRegistry->getStockItem($product->getId(), $product->getStore()->getWebsiteId());

I've change this ↓

$stockRegistry = $objectManager->get('Magento\CatalogInventory\Api\StockRegistryInterface');
$stockitem = $stockRegistry->getStockItem($product->getId(), $product->getStore()->getWebsiteId());
if($stockitem->getQty() == 0) continue;

Into this ↓

use Magento\InventorySalesApi\Api\Data\SalesChannelInterface;

$productSalableQty = $objectManager->get('\Magento\InventorySalesApi\Api\GetProductSalableQtyInterface');
$stockResolver = $objectManager->get('\Magento\InventorySalesApi\Api\StockResolverInterface');

$stockId    = $stockResolver->execute(SalesChannelInterface::TYPE_WEBSITE, $product->getStore()->getWebsite()->getCode())->getStockId();
$stockQty   = $productSalableQty->execute($product->getSku(), $stockId);

if($stockQty == 0) continue;

Similar reference of this is in this link

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.