Hiding Configurable Products When All Options Are Out of Stock
In Magento 2, configurable products offer a great deal of flexibility in handling multiple product options and variations. However, one common challenge merchants face is how to handle the visibility of a configurable product when all its associated simple products are out of stock.
This issue was recently raised by Murali on our support forums, where he asked how to hide an entire configurable product if none of its options were available.
Understanding Magento’s Configuration
In Magento 2, the visibility of a configurable product is determined by several factors:
-
Stock Status: If any of the associated simple products have a stock status set to ‘Out of Stock’ or ‘Low Stock’, they won’t be displayed in the dropdown menu on the frontend.
-
Enable/Disable: By default, if all the options are out of stock, Magento will not display the ‘Add to Cart’ button and the option dropdown for that product.
-
Is Salable: Magento checks if a product is salable before displaying it. If a product is out of stock and there’s no backorder allowed, it won’t be displayed.
However, Murali’s requirement was slightly different - he wanted to completely hide the parent configurable product from the catalog when all its options were out of stock.
Hiding Configurable Products Programmatically
To achieve this, we need to override the getIsSalable method in the Magento\Catalog\Model\Product class. This method is responsible for checking if a product is salable or not.
Here’s how you can do it:
- Create an observer plugin for the
catalog_product_get_is_salableevent. - In the
afterExecutemethod, check if all the associated simple products are out of stock. - If so, set the salability flag to false and return from the observer.
Here’s a code snippet that demonstrates this:
class HideConfigurableProductWhenOptionsOutOfStock implements \Magento\Framework\Event\ObserverInterface
{
public function afterExecute(\Magento\Framework\Event\Observer $observer)
{
$product = $observer->getProduct();
if ($product->getTypeId() === 'configurable') {
$childProducts = $product->getTypeInstance()->getUsedProducts($product);
foreach ($childProducts as $child) {
if ($child->getStockItem()->getQty() <= 0) {
return false;
}
}
}
return true;
}
}
Don’t forget to enable the event plugin in your module’s etc/events.xml file:
<event name="catalog_product_get_is_salable">
<observer name="hide_configurable_product_when_options_out_of_stock" instance="Vendor\ModuleName\Observer\HideConfigurableProductWhenOptionsOutOfStock"/>
</event>
Remember to replace “Vendor\ModuleName” with your actual vendor and module names.
Conclusion
Hiding a configurable product when all its options are out of stock requires a bit of customization in Magento 2. By using an observer plugin, we can override the salability check for configurable products and ensure that they don’t appear on the frontend if none of their options are available.
This approach respects Magento’s built-in logic while providing the specific functionality Murali required.
For more detailed guidance on Magento development, consider checking out our Magento Development Services page at Laravel Company. Our team of experienced developers is always ready to provide expert advice and hands-on support for your e-commerce projects.
Happy coding!