Question

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!

Was it helpful?

Solution

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

OTHER TIPS

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top