How do I get bundle option selection SKU?

Viewed 10682
<?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.

3 Answers
Magento 2 get bundle options with their selections details.

Class BundleItemDetails
   public function __construct(
        \Magento\Catalog\Model\ProductRepository; $productRepository
    )
    {
        $this->productRepository = $productRepository;
     }

 public function execute(){
       $product  = $this->productRepository->get("test-bundle-product");
       $optionsCollection = $product->getTypeInstance(true)
                                    ->getOptionsCollection($product);
       $optionDetails = [];
       foreach ($optionsCollection as $option){
         $selections = $product->getTypeInstance(true)
                               ->getSelectionsCollection(
                                $option->getOptionId(),$product
                             );
         //selection details by optionids
          foreach ($selections as $selection) {
                  $optionDetails[$option->getOptionId()] = $selection->getSku();
          }
      } 
  }


}
Related