How to get list of all selected items of bundle product on detail page magento2
-
you want all items for that bundle product in any page, right?– Sarvesh PatelCommented Feb 19, 2020 at 7:04
-
yes all selected items for that bundle product– Junaid AshfaqCommented Feb 19, 2020 at 7:12
-
please check answer I uodated. and you add more details what you need in array ok.– Sarvesh PatelCommented Feb 19, 2020 at 7:13
-
it give me Mage error in magento 2– Junaid AshfaqCommented Feb 19, 2020 at 7:14
Add a comment
|
2 Answers
Pleas try below code.
$product_id = 'your bundle product id';
$_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$store_id = 1;
$options = $_objectManager->create('Magento\Bundle\Model\Option')
->getResourceCollection()
->setProductIdFilter($product_id)
->setPositionOrder();
$options->joinValues($store_id);
$typeInstance = $_objectManager->get('Magento\Bundle\Model\Product\Type');
$selections = $typeInstance->getSelectionsCollection($typeInstance->getOptionsIds($product), $product);
foreach($selections as $selection){
if($product->getSku() == $selection->getSku()){
return $selection->getSelectionQty();
}
}
print_r($selections);
<ul>
<?php
$productId = 17; //any bundle product id
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Catalog\Model\Product')->load($productId);
//get all the selection products used in bundle product.
$selectionCollection = $product->getTypeInstance(true)
->getSelectionsCollection(
$product->getTypeInstance(true)->getOptionsIds($product),
$product
);
foreach ($selectionCollection as $proselection) {
$selectionArray = [];
echo '<li>';
echo $selectionArray['selection_product_name'] = $proselection->getName();
echo '<br>';
echo $selectionArray['selection_product_price'] = $proselection->getSelectionQty();
echo '</li>';
}
?>
</ul>
this work for me