6

我们都知道,magento 中的可配置产品与简单产品相关联。

如果与可配置产品关联的简单产品变为Inventory = 0,则表示可配置产品缺货

所以问题是我如何检测可配置产品是否缺货?我想检测,所以我可以在前端显示“缺货”文本。

像这样的东西

if($configurable_product->isOutOfStock()) {
   echo "Out of Stock";
}

我怎样才能在 Magento 中做到这一点?

4

5 回答 5

7
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
                }
            }
于 2014-08-15T08:45:46.057 回答
0
$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 的产品并不总是缺货。

于 2014-08-15T15:43:50.597 回答
0
$_productCollection = Mage::getResourceModel('catalog/product_collection')
->addAttributeToFilter('type_id', array('eq' => 'configurable'));

Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($_productCollection);

这仅显示库存的可配置产品。

于 2015-05-01T07:30:49.553 回答
0

只是对 Quovadisqc 的回答稍作更新/更正。定义 $qty 时应该是

$qty = $stockItem->getData('qty'); // correct

而不是目前存在的,

$qty = $stockItem->setData('qty'); // incorrect

我会将此作为评论发布,但我没有足够的代表。

于 2015-07-24T14:19:45.407 回答
0

在产品的 foreach 循环中,以下 if 语句有效。

if ($product->getIsInStock() === '1' && $product->isSaleable() === true) {
    echo 'this product is in stock';
}
于 2015-08-20T14:21:40.680 回答