Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
quote_with_bundle_product.php
Go to the documentation of this file.
1 <?php
7 require __DIR__ . '/../../../Magento/Bundle/_files/product.php';
8 
10 $productRepository = $objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class);
12 $product = $productRepository->get('bundle-product');
13 
15 //Load options
16 $typeInstance = $product->getTypeInstance();
17 $typeInstance->setStoreFilter($product->getStoreId(), $product);
18 $optionCollection = $typeInstance->getOptionsCollection($product);
19 $selectionCollection = $typeInstance->getSelectionsCollection($typeInstance->getOptionsIds($product), $product);
20 
24 foreach ($optionCollection as $option) {
26  $selection = $selectionCollection->getFirstItem();
27  $bundleOptions[$option->getId()] = $selection->getSelectionId();
28  $bundleOptionsQty[$option->getId()] = 1;
29 }
30 
31 $requestInfo = new \Magento\Framework\DataObject(
32  ['qty' => 1, 'bundle_option' => $bundleOptions, 'bundle_option_qty' => $bundleOptionsQty]
33 );
34 
37 $cart->addProduct($product, $requestInfo);
38 $cart->getQuote()->setReservedOrderId('test_cart_with_bundle');
39 $cart->save();
40 
43 $objectManager->removeSharedInstance(\Magento\Checkout\Model\Session::class);
foreach($optionCollection as $option) $requestInfo
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60