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.
Success, after some trial and error, the following code will work for drop_down, radio and checkboxes and load the options SKU.
EDIT: Going forward the only thing this is missing is that the sku values are not reloaded when clicking the edit item link from the main cart page. The script will fire on the first change made in the options by the user but I haven't figured out how to fire it on page load so the sku with options shows.
If someone can advise how this would be done with an example it would be much appreciated.
</script>
<!-- begin custom code -->
<dl>
<?php $options_to_sku = array();?>
<?php foreach($_options as $_option): ?>
<?php echo $this->getOptionHtml($_option) ?>
<?php
if ($_option->getType() == 'drop_down' || $_option->getType() == 'checkbox' || $_option->getType() == 'radio')
{
$options_to_sku["{$_option->getId()}"] = array();
foreach ($_option->getValues() as $k => $v)
{
$options_to_sku["{$_option->getId()}"]["{$v->getData('option_type_id')}"] = $v->getData('sku');
}
}
?>
<?php endforeach; ?>
</dl>
<script type="text/javascript">
$("sku-suffix-container").update("<strong>Configuration: <?php echo $this->getProduct()->getSku(); ?></strong>");
var options_to_sku = <?php echo json_encode($options_to_sku);?>;
$$('.product-custom-option').each(function(elm) {
$(elm).observe('change', function() {
var sku_suffix = '';
$$('.product-custom-option').each(function(opt) {
if ($(opt).value.length==0 || $(opt).checked==false) // no value selected
return;
var build_opt_id = $(opt).readAttribute('id').replace('select_','').replace('options_','');
var opt_id = build_opt_id.replace(/\_.*/,'');
if (!!!options_to_sku[opt_id]) // no option values array found
return;
if (!!!options_to_sku[opt_id][$(opt).value]) // no sku found matching value id
return;
sku_suffix += '-' + options_to_sku[opt_id][$(opt).value] // add to suffix
});
$("sku-suffix-container").update("<strong>Configuration: <?php echo $this->getProduct()->getSku(); ?></strong>" + (sku_suffix) + "TestText");
console.log(sku_suffix);
});
});
</script>
<!-- end custom code -->
<!--<dl>
<?php foreach($_options as $_option): ?>
<?php echo $this->getOptionHtml($_option) ?>
<?php endforeach; ?>
</dl>-->
<?php endif; ?>
Best Answer
Try this: