Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get bundle option selection SKU?

Tags:

magento

<?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>";                
            }  
        }           
    }       
}

enter image description here

I am able to get the title, qty and the price of product, I also want to get the product SKU.

like image 819
Mukesh Avatar asked Feb 14 '23 07:02

Mukesh


2 Answers

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 />';
            }
        }
    }
}

?>
like image 182
ToxaBes Avatar answered Feb 28 '23 02:02

ToxaBes


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;
like image 37
Prince Patel Avatar answered Feb 28 '23 01:02

Prince Patel