Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
order_bundle_products.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
11 use Magento\Quote\Api\Data\AddressInterfaceFactory;
14 
16 $cartRepository = Bootstrap::getObjectManager()->get(CartRepositoryInterface::class);
18 $productRepository = Bootstrap::getObjectManager()->get(ProductRepositoryInterface::class);
20 $cartManagement = Bootstrap::getObjectManager()->get(CartManagementInterface::class);
22 $addressFactory = Bootstrap::getObjectManager()->get(AddressInterfaceFactory::class);
23 
25  'SKU-BUNDLE-1' => ['qty' => 2, 'options_qty' => [3, 4]],
26  'SKU-BUNDLE-2' => ['qty' => 3, 'options_qty' => [5, 6]]
27 ];
28 
29 $cartId = $cartManagement->createEmptyCart();
31 $cart->setCustomerEmail('[email protected]');
32 $cart->setCustomerIsGuest(true);
35  [
36  'data' => [
37  AddressInterface::KEY_COUNTRY_ID => 'US',
38  AddressInterface::KEY_REGION_ID => 15,
39  AddressInterface::KEY_LASTNAME => 'Doe',
40  AddressInterface::KEY_FIRSTNAME => 'John',
41  AddressInterface::KEY_STREET => 'example street',
42  AddressInterface::KEY_EMAIL => '[email protected]',
43  AddressInterface::KEY_CITY => 'example city',
44  AddressInterface::KEY_TELEPHONE => '000 0000',
45  AddressInterface::KEY_POSTCODE => 12345
46  ]
47  ]
48 );
49 $cart->setReservedOrderId('test_order_bundle_1');
50 $cart->setBillingAddress($address);
51 $cart->setShippingAddress($address);
52 $cart->getPayment()->setMethod('checkmo');
53 $cart->getShippingAddress()->setShippingMethod('flatrate_flatrate');
54 $cart->getShippingAddress()->setCollectShippingRates(true);
55 $cart->getShippingAddress()->collectShippingRates();
56 
57 foreach ($itemsToBuy as $sku => $qtyData) {
58  $product = $productRepository->get($sku);
59  $options = $product->getTypeInstance()->getOptions($product);
60  $optionsData = [];
61  $optionsQtyData = [];
62  $i = 0;
63  foreach ($options as $option) {
64  $optionsData[$option->getId()] = $option->getId();
65  $optionsQtyData[$option->getId()] = $qtyData['options_qty'][$i];
66  $i++;
67  }
68  $requestData = [
69  'product' => $product->getProductId(),
70  'qty' => $qtyData['qty'],
71  'bundle_option' => $optionsData,
72  'bundle_option_qty' => $optionsQtyData,
73  ];
74  $request = new \Magento\Framework\DataObject($requestData);
75  $cart->addProduct($product, $request);
76 }
77 
78 $cartRepository->save($cart);
79 $cartManagement->placeOrder($cart->getId());
$i
Definition: gallery.phtml:31