Showing Backorder Status on Magento Product Page
Magento provides extensive control over inventory management, including support for backordered items. However, by default, it doesn’t display this information on the frontend product page. In this guide, I’ll walk you through the process of adding a message to indicate when an item is currently on backorder.
Understanding Magento’s Inventory System
Before we dive into the code, let’s clarify how Magento handles stock status. There are three main statuses:
- In Stock: Available for immediate shipping.
- Out of Stock: Currently unavailable, but previous stock exists.
- Backordered: Not available now, but can be ordered with delayed delivery.
Checking the Product’s Backorder Status
To determine if a product is on backorder, we need to check its is_backorders_allowed attribute and compare it with the current quantity in stock. Here’s how you can do this:
$product = $block->getProduct();
$allowBackorders = $product->getIsBackordersAllowed();
$stockItem = $product->getStockItem();
// If backorders are allowed and we have a negative quantity...
if ($allowBackorders && $stockItem->getQty() < 0) {
// The product is on backorder
}
Adding the Backorder Message to the Product Page
Now that you know how to identify a backordered item, let’s add a message to your product view page. Open your catalog/product/view.phtml file (usually located in app/design/frontend/your_package/your_theme/templates/catalog/product/).
To add a conditional message below the product description and price, use this code:
<?php if ($block->getProduct()->isAvailable()): ?>
<div class="availability">
<?php if ($block->getProduct()->isInStock()): ?>
<span class="in-stock"><?= $block->__('In stock') ?></span>
<?php elseif ($block->getProduct()->getIsBackordersAllowed() && $block->getProduct()->getStockItem()->getQty() < 0): ?>
<span class="back-order"><?= $block->__('This item is currently on backorder. Please allow extra time for shipping.') ?></span>
<?php else: ?>
<span class="out-of-stock"><?= $block->__('Out of stock') ?></span>
<?php endif; ?>
</div>
<?php endif; ?>
In this code, we first check if the product is available. If it’s not available but backorders are allowed and there’s a negative quantity, we display a custom message indicating that the item is on backorder.
Styling the Backorder Message
To distinguish the backorder message from other stock status messages, you can add some CSS styling to your theme:
.back-order {
color: #ff0000; /* Set the text color to red */
font-weight: bold;
}
This will make the backorder message stand out on your product pages.
Conclusion
Displaying backorder status information on the Magento frontend is a great way to manage customer expectations and reduce confusion during the purchasing process. By following this guide, you can easily add a clear indicator for backordered items without changing any core files.
Remember, customizing Magento templates requires careful version management to avoid conflicts during updates. It’s always recommended to use child themes for modifications like these.
For more advanced inventory management techniques and best practices, check out our Magento Inventory Management Guide at Laravel Company.
Happy coding!