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