0

How to get list of all selected items of bundle product on detail page magento2

4
  • you want all items for that bundle product in any page, right? Commented Feb 19, 2020 at 7:04
  • yes all selected items for that bundle product Commented Feb 19, 2020 at 7:12
  • please check answer I uodated. and you add more details what you need in array ok. Commented Feb 19, 2020 at 7:13
  • it give me Mage error in magento 2 Commented Feb 19, 2020 at 7:14

2 Answers 2

1

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); 
8
  • how can i write in magneto 2 Commented Feb 19, 2020 at 7:13
  • above code is for magento 1 Commented Feb 19, 2020 at 7:13
  • it give me Mage error in magento 2 Commented Feb 19, 2020 at 7:13
  • okay wait I will update you. Commented Feb 19, 2020 at 7:14
  • please try new one Commented Feb 19, 2020 at 7:18
1
<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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.