我们都知道,magento 中的可配置产品与简单产品相关联。
如果与可配置产品关联的简单产品变为Inventory = 0,则表示可配置产品缺货
所以问题是我如何检测可配置产品是否缺货?我想检测,所以我可以在前端显示“缺货”文本。
像这样的东西
if($configurable_product->isOutOfStock()) {
echo "Out of Stock";
}
我怎样才能在 Magento 中做到这一点?
我们都知道,magento 中的可配置产品与简单产品相关联。
如果与可配置产品关联的简单产品变为Inventory = 0,则表示可配置产品缺货
所以问题是我如何检测可配置产品是否缺货?我想检测,所以我可以在前端显示“缺货”文本。
像这样的东西
if($configurable_product->isOutOfStock()) {
echo "Out of Stock";
}
我怎样才能在 Magento 中做到这一点?
if (!$configurable->isSaleable() ||$configurable_product->getIsInStock()==0){
// out of stock
}
检查子简单产品:
$allProducts = $configurable->getTypeInstance(true)
->getUsedProducts(null, $configurable);
foreach ($allProducts as $product) {
if (!$product->isSaleable()|| $product->getIsInStock()==0) {
//out of stock for check child simple product
}
}
$product = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);
$stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product->getId());
$qty = $stockItem->getData('qty');
$inStock = $stockItem->getData('is_in_stock');
if ($qty < 1 || $inStock == 0) {
// OutOfStock
}
我更喜欢仔细检查 qty,因为根据配置设置,qty == 0 的产品并不总是缺货。
$_productCollection = Mage::getResourceModel('catalog/product_collection')
->addAttributeToFilter('type_id', array('eq' => 'configurable'));
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($_productCollection);
这仅显示库存的可配置产品。
只是对 Quovadisqc 的回答稍作更新/更正。定义 $qty 时应该是
$qty = $stockItem->getData('qty'); // correct
而不是目前存在的,
$qty = $stockItem->setData('qty'); // incorrect
我会将此作为评论发布,但我没有足够的代表。
在产品的 foreach 循环中,以下 if 语句有效。
if ($product->getIsInStock() === '1' && $product->isSaleable() === true) {
echo 'this product is in stock';
}