1

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!

1

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

  • 1
    Excellent! I've been looking all over for this solution and this is the only one that's worked. Thank you so much. – Thomas Harding Apr 11 at 10:07
  • I'm glad that it helped – Karsai Péter Apr 12 at 11:16
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.

  • Thanks for your input, but it ended up being much more complicated than that. See my answer for details. – Karsai Péter Apr 3 at 17:19

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.