cancel
Showing results for 
Search instead for 
Did you mean: 

How to get Bundle Product Selected Options From Quote ?

How to get Bundle Product Selected Options From Quote ?

I am creating a Custom APi to retrieve products in cart how can i get the bundle product details as displayed in web currently how i do this

 

$childrens = $item->getChildren();
    public function getBundleData($childrens) {
        $data = [];
        $count = 0;
        foreach($childrens as $children) {
            $data[$count]['id'] = $children->getId();
            $data[$count]['name'] = $children->getName();
            $data[$count]['price'] = $children->getData('price_incl_tax');
            $data[$count]['quantity'] = $children->getQty();
            $count++;             
        }
        return $data;
    }

 

 

But I also want to get the option label option id selected option Id etc as displayed in below figure please help thanks in advanced

 

hassan.png

 

1 REPLY 1

Re: How to get Bundle Product Selected Options From Quote ?

Hello @hassanzami1061 

 

Try This Code:

 

 

use Magento\Quote\Model\Quote\Item;

class YourCustomClass
{
protected $itemModel;

public function __construct(Item $itemModel)
{
$this->itemModel = $itemModel;
}

public function getBundleData($childrens)
{
$data = [];
$count = 0;

foreach ($childrens as $children) {
$data[$count]['id'] = $children->getId();
$data[$count]['name'] = $children->getName();
$data[$count]['price'] = $children->getData('price_incl_tax');
$data[$count]['quantity'] = $children->getQty();

// Get bundle options and selections
$productOption = $children->getProductOption();
if ($productOption && isset($productOption['extension_attributes']['bundle_options'])) {
foreach ($productOption['extension_attributes']['bundle_options'] as $option) {
$data[$count]['bundle_options'][] = [
'option_label' => $option['label'],
'option_id' => $option['option_id'],
'selected_option_id' => $option['value']['value_index'],
'selected_option_label' => $option['value']['title'],
];
}
}

$count++;
}

return $data;
}
}

 

  1. Inject the Item class in your custom class constructor.
  2. Inside the loop, get the product options using $children->getProductOption(). This will give you an array containing option data.
  3. Loop through the bundle options using $option['extension_attributes']['bundle_options'] and extract the required information like option_label, option_id, selected_option_id, and selected_option_label.