The issue at hand is that $this->getItems()
returns only visible items ( see Mage_Checkout_Block_Cart_Abstract::getItems()
. Visible items include the configurable parent but not the children.
In that case you will need to do something akin to:
foreach($this->getQuote()->getAllItems() as $_item){
//...
}
This will return all of the items including both the parent configurable and the child. To filter just for the simple child you'd do something like:
foreach($this->getQuote()->getAllItems() as $_item){
if($_item->getProduct()->getTypeId()=='simple' && $_item->getParentId()){
//then it's a simple child!
}
}
As an aside, in the future, you needn't call load
again on Product ID as you already have the product with $_item->getProduct()
. If product information isn't loading in a particular scope try adding it to the collection.
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.
Best Answer
For Future Seekers:
Try:
$customOptions = $options['bundle_options']
It will give you array list of options like this: (this is json representation of data)