Displaying Stock Availability on Magento Product Grid
As a Magento developer, I often field requests from clients who want to display stock availability information directly on the product grid page, without having to click through to each individual product. This is particularly useful for wholesale businesses that carry both in-stock and out-of-stock items.
Understanding Magento’s Default Behavior
By default, Magento does not show stock status on the category view. The platform focuses on providing a clean, uncluttered shopping experience by hiding this information until customers reach the product detail page.
However, as you’ve discovered, there are situations where it makes sense to modify this behaviorâespecially for businesses that use the Magento grid primarily as a catalog rather than a traditional e-commerce storefront. Let’s explore how to achieve your desired outcome.
The Technical Approach
To display stock status on the category grid view, we’ll need to override the default category product collection and add a new column for the stock status attribute.
Step 1: Create an Observer to Modify the Collection
First, let’s create a simple observer that will be triggered when Magento generates the category product list. This observer will modify the collection to include our custom column.
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/event.xsd">
<event name="catalog_category_product_collection_load_after">
<observer name="add_stock_status_to_grid" instance="Vendor\YourModule\Model\Observer\AddStockStatusToGrid"/>
</event>
</config>
In your observer class, you can modify the collection as follows:
public function execute(\Magento\Framework\Event\Observer $observer)
{
$collection = $observer->getCollection();
$collection->addAttributeToSelect('stock_status');
}
Step 2: Displaying the Stock Status Column
Next, we need to update your category view to include this new column. This involves editing the existing XML layout file for your category template.
In the catalog_category_view.xml file located in your theme folder (e.g., app/design/frontend/Vendor/YourTheme/Magento_Catalog/layout/catalog_category_view.xml), add a new column with the following structure:
<referenceBlock name="category.product.list">
<arguments>
<argument name="columnCount" xsi:type="number">4</argument> <!-- Adjust this as needed for your grid layout -->
</arguments>
<block class="Magento\Framework\View\Element\Template" name="stock_status_column" template="Magento_Theme::html/stock_status.phtml">
<arguments>
<argument name="product_list" xsi:type="object">Magento\Framework\Pricing\Render\RendererPool</argument>
</arguments>
</block>
</referenceBlock>
Step 3: Creating the Column Template
Finally, create a new template file named stock_status.phtml in your theme folder (e.g., app/design/frontend/Vendor/YourTheme/Magento_Theme/templates/html/stock_status.phtml).
In this template, you can display the stock status using a simple if-else statement:
<?php
$product = $block->getProduct();
?>
<?php if ($product->isAvailable()): ?>
<span class="in-stock"><?= __('In Stock') ?></span>
<?php elseif (!$product->isSalable()): ?>
<span class="out-of-stock"><?= __('Out of Stock') ?></span>
<?php endif; ?>
Conclusion
Displaying stock status on the category product grid in Magento requires a combination of event observers and layout modifications. By following this approach, you can provide your customers with clear visibility into product availability directly from the category view.
Remember that customizing core functionality like this should be done carefully, as it may impact upgrade paths. Always test thoroughly and consider using a child theme or module to preserve your changes across upgrades.
For more advanced Magento customizations and best practices, I highly recommend checking out our comprehensive guides at Laravel Company.
Happy coding!