Magento 2 商品库存状态(缺货/有货)功能定制需求(参照截图)
Got it, let's walk through how to implement stock status (In Stock/Out of Stock) related features in Magento 2. I'll cover the most common scenarios you might need based on typical inventory requirements:
1. Customize Stock Status Display on Product Pages
Magento has a default stock status display, but if you want to tweak its appearance or add custom logic, here's how:
- First, locate the product view template in your theme:
app/design/frontend/[YourThemeVendor]/[YourTheme]/Magento_Catalog/templates/product/view/type/default.phtml - Replace or add the stock status section with this code (adjust styling as needed):
<?php $_product = $block->getProduct(); ?> <?php $stockItem = $_product->getExtensionAttributes()->getStockItem(); ?> <div class="stock-status-container"> <?php if ($_product->isAvailable()): ?> <?php if ($stockItem->getQty() <= 5): // Show low stock alert if qty is 5 or less ?> <div class="stock low-stock" title="<?= /* @noEscape */ __('Availability') ?>"> <span><?= /* @noEscape */ __('Low Stock - Only %1 left!', $stockItem->getQty()) ?></span> </div> <?php else: ?> <div class="stock available" title="<?= /* @noEscape */ __('Availability') ?>"> <span><?= /* @noEscape */ __('In Stock') ?></span> </div> <?php endif; ?> <?php else: ?> <div class="stock unavailable" title="<?= /* @noEscape */ __('Availability') ?>"> <span><?= /* @noEscape */ __('Out of Stock') ?></span> </div> <?php endif; ?> </div> - Add custom CSS to your theme's stylesheet for better visibility:
.stock-status-container { margin: 10px 0; } .stock.available { color: #2ecc71; font-weight: bold; } .stock.low-stock { color: #f39c12; font-weight: bold; } .stock.unavailable { color: #e74c3c; font-weight: bold; }
2. Show Stock Status on Category/Product List Pages
To display stock status alongside each product in category lists:
- Open the list template file:
app/design/frontend/[YourThemeVendor]/[YourTheme]/Magento_Catalog/templates/product/list.phtml - Insert this snippet inside the product loop (where you want the status to appear):
<?php $_product = $_item; ?> <?php if ($_product->isAvailable()): ?> <span class="product-stock available"><?= /* @noEscape */ __('In Stock') ?></span> <?php else: ?> <span class="product-stock unavailable"><?= /* @noEscape */ __('Out of Stock') ?></span> <?php endif; ?> - Add matching CSS to your theme:
.product-stock { font-size: 0.9em; margin-left: 8px; }
3. Disable Add to Cart for Out-of-Stock Products
While Magento does this by default, you can customize the disabled button text/behavior:
- In the same product view template (default.phtml), modify the Add to Cart section:
<?php if ($_product->isAvailable()): ?> <button type="submit" title="<?= $block->escapeHtmlAttr(__('Add to Cart')) ?>" class="action tocart primary"> <span><?= $block->escapeHtml(__('Add to Cart')) ?></span> </button> <?php else: ?> <button type="button" disabled title="<?= $block->escapeHtmlAttr(__('Out of Stock')) ?>" class="action tocart primary disabled"> <span><?= $block->escapeHtml(__('Out of Stock - Unavailable')) ?></span> </button> <?php endif; ?>
4. Custom Stock Status Logic with a Plugin
If you need complex logic (like hiding out-of-stock products entirely, or custom statuses), use a plugin to override the stock check:
- Create a plugin class in your custom module:
<?php namespace YourVendor\InventoryCustom\Plugin\Catalog\Model; use Magento\Catalog\Model\Product as Subject; use Magento\CatalogInventory\Api\StockRegistryInterface; class Product { protected $stockRegistry; public function __construct(StockRegistryInterface $stockRegistry) { $this->stockRegistry = $stockRegistry; } public function afterIsAvailable(Subject $subject, $result) { $stockItem = $this->stockRegistry->getStockItem($subject->getId()); // Example: Mark product as unavailable if qty is 0 AND backorders are disabled if (!$stockItem->getQty() && !$stockItem->getBackorders()) { return false; } // Add your custom logic here return $result; } } - Register the plugin in your module's
di.xml:<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Magento\Catalog\Model\Product"> <plugin name="yourvendor_inventorycustom_product_isavailable" type="YourVendor\InventoryCustom\Plugin\Catalog\Model\Product"/> </type> </config>
Important Notes
- After making changes, run these commands to apply them:
bin/magento setup:upgrade bin/magento setup:static-content:deploy -f bin/magento cache:flush - If you're using a third-party theme, ensure you're editing the correct template files in your theme's directory (not the default Magento files).
- Always test changes in a staging environment before pushing to production.
内容的提问来源于stack exchange,提问作者JatinBhattPhp
相关产品推荐
相关产品推荐

