<?php
require_once('app/Mage.php'); //Path to Magento
umask(0);
Mage::app("default");
$orderNumber = 260038;
$order = Mage::getModel('sales/order')->loadByIncrementId($orderNumber);
foreach ($order->getAllItems() as $item){
$productOptions = $item->getProductOptions();
echo $product_id = $item->product_id;
$_product=Mage::getModel('catalog/product')->load($product_id);
if ($_product->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_BUNDLE) {
if (isset($productOptions['bundle_options']))
{
foreach ($productOptions['bundle_options'] as $productOption)
{
echo $value = $productOption['value'][0]['title'];
echo ' || ';
echo $value = $productOption['value'][0]['qty'];
echo ' || ';
echo $value = $productOption['value'][0]['price'];
echo "<br>";
}
}
}
}
I am able to get the title, qty and the price of product, I also want to get the product SKU.
Bundle products can have options, options can have selections. This is 'two-tier' structure. If you just want to get all selections without options, you can use something like this:
$selections = $product->getTypeInstance(true)
->getSelectionsCollection($product->getTypeInstance(true)
->getOptionsIds($product), $product);
foreach($selections as $selection){
echo $selection->getSku();
}
But if you want get full information about options and their selections, use next way (based on your example):
<?php
require_once('app/Mage.php'); //Path to Magento
umask(0);
Mage::app("default");
$orderNumber = 260038;
$order = Mage::getModel('sales/order')->loadByIncrementId($orderNumber);
$store_id = $order->getStoreId();
foreach ($order->getAllItems() as $item){
$product = Mage::getModel('catalog/product')->setStoreId($store_id)->load($item->product_id);
$options = Mage::getModel('bundle/option')->getResourceCollection()
->setProductIdFilter($item->product_id)
->setPositionOrder();
$options->joinValues($store_id);
$selections = $product->getTypeInstance(true)
->getSelectionsCollection($product->getTypeInstance(true)
->getOptionsIds($product), $product);
foreach ($options->getItems() as $option) {
$option_id = $option->getId();
echo 'Option: ' . $option->getTitle() . ' [id: ' . $option_id . ']<br />';
foreach($selections as $selection){
if($option_id == $selection->getOptionId()){
$selection_id = $selection->getId();
$selection_name = $selection->getName();
$selection_qty = $selection->getSelectionQty();
$selection_sku = $selection->getSku();
$selection_product_id = $selection->getProductId();
$selection_weight = $selection->getWeight();
$selection_price = $selection->getPrice();
$data = 'Selection Name: ' . $selection_name;
$data .= ', SKU: ' . $selection_sku;
$data .= ', Qty: ' . $selection_qty;
$data .= ', ID: ' . $selection_id;
$data .= ', Product ID: ' . $selection_product_id;
$data .= ', Weight: ' . $selection_weight;
$data .= ', Price: ' . $selection_price;
echo $data . '<br />';
}
}
}
}
?>
Here we go,
To get product sku by selected option id:
$optionId = "selected option id";
$bundleTable = Mage::getSingleton('core/resource')->getTableName('catalog_product_bundle_selection');
$collection=Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToSelect(array('name', 'price'));
$collection->getSelect()->joinLeft(array('bundleselect'=> $bundleTable),"entity_id = bundleselect.product_id","product_id");
$collection->getSelect()->where(" bundleselect.selection_id IN (".$optionId.") " );
$origPrice = '0';
foreach($collection as $prod) {
$origPrice += $prod->getSku();
}
echo $origSku;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With