We use several bundled products, and I'm having trouble displaying the stock value on the product grid. I don't even need values, just in stock
or out of stock
.
Take the following example:
- iPhone, Black, $400, 2 in stock
- iPhone, White, $410, 0 in stock
- iPhone, Blue, $420, 1 in stock
With the following code I can get the prices of the bundled products and select the lowest price ($400). I thought I could use this to get the available quantities as well. I found the value of stock items in the field qty
, so I tried getQty()
instead of getPrice()
. That returns the stock for the bundle product, the container, which is 0. I need the sum of the stock (3), or the stock value of the bundle item with the highest stock (2).
How can I get the stock value for each bundled product?
if ($_product->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_BUNDLE) { $optionCol= $_product->getTypeInstance(true)->getOptionsCollection($_product); $selectionCol= $_product->getTypeInstance(true) ->getSelectionsCollection($_product->getTypeInstance(true)->getOptionsIds($_product),$_product); $optionCol->appendSelections($selectionCol); $price = $_product->getPrice(); foreach ($optionCol as $option) { if($option->required) { $selections = $option->getSelections(); $minPrice = min(array_map(function ($s) { return $s->price; }, $selections)); if($_product->getSpecialPrice() > 0) { $minPrice *= $_product->getSpecialPrice()/100; } $price += round($minPrice,2); } } }