5

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);
        }
    }
}
5
+25

Here is an example how to access the is salable state or the stock quantity of all associated child products.
this first method is simply to fetch the child product ids from the bundled product.

function getBundledProductChildIds(Mage_Catalog_Model_Product $product)
{
    $childrenIds = [];
    if ($product->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_BUNDLE) {
        $childrenIds = array_reduce(
            $product->getTypeInstance(true)->getChildrenIds($product->getId()),
            function (array $reduce, $value) {
                return array_merge($reduce, $value);
            }, []);
    }
    return $childrenIds;
}

The next example gives you the in stock or out of stock for each child product.

$bundledProduct = Mage::getModel('catalog/product')->load(BUNDLED_PRODUCT_ID);

// Just get the is salable state of the child products
$childProducts = Mage::getResourceModel('catalog/product_collection')
    ->addIdFilter(getBundledProductChildIds($bundledProduct));
foreach ($childProducts as $child) {
    printf("%s -> %s stock\n", $child->getId(), ($child->getIsSalable() ? 'in' : 'out of'));
}

The final example gives you the stock qty of each child product.
Note that the key difference is that the require_stock_items flag is set on the collection before it is loaded.

$bundledProduct = Mage::getModel('catalog/product')->load(BUNDLED_PRODUCT_ID);    

// Get the inventory qty of each child item
$childProducts = Mage::getResourceModel('catalog/product_collection')
    ->addIdFilter(getBundledProductChildIds($bundledProduct))
    ->setFlag('require_stock_items', true);
foreach ($childProducts as $child) {
    printf("%s -> stock qty: %d\n", $child->getId(), $child->getStockItem()->getQty());
}

printf("Total stock item qty sum: %d\n", array_reduce(
    $childProducts->getItems(),
    function ($sum, $child) {
        return $sum + $child->getStockItem()->getQty();
    }, 0));

printf("Highest stock qty: %d\n", array_reduce(
    $childProducts->getItems(),
    function ($max, $child) {
        return max($max, $child->getStockItem()->getQty();
    }, 0));

That should hopefully enable you to get what you need.

  • Great answer! Thanks for the excellent code. In the first function you use [], which caused an error because of my older version of PHP. In that case you can replace those two characters with array() and it should work. I haven't used the third part, but I guess that will work as well. – SPRBRN Feb 3 '15 at 15:59
0

I'll post some code that works for me. Hopefully you can customize to your fit :D

$bundled_product = Mage::getModel('catalog/product');       
$bundled_product->load(YOUR_BUNDLED_PRODUCT_ID);


$selectionCollection = $bundled_product->getTypeInstance(true)->getSelectionsCollection(
    $bundled_product->getTypeInstance(true)->getOptionsIds($bundled_product), $bundled_product
);

$bundled_items = array();
foreach($selectionCollection as $option) 
{
    $bundled_items[] = $option->product_id;
}

foreach($bundled_Items as $itemId){
    $product = $this->getProductById($itemId);
    $productquantity = $product->getQty();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.