Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
quote_with_bundle.php
Go to the documentation of this file.
1 <?php
9 $simpleProducts[] = $objectManager->create(\Magento\Catalog\Model\Product::class)
10  ->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
11  ->setAttributeSetId(4)
12  ->setWebsiteIds([1])
13  ->setName('Simple Product 1')
14  ->setSku('simple-1')
15  ->setPrice(10)
16  ->setDescription('Description with <b>html tag</b>')
17  ->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
18  ->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
19  ->setCategoryIds([2])
20  ->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1])
21  ->save();
22 
23 $simpleProducts[] = $objectManager->create(\Magento\Catalog\Model\Product::class)
24  ->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
25  ->setAttributeSetId(4)
26  ->setWebsiteIds([1])
27  ->setName('Simple Product 2')
28  ->setSku('simple-2')
29  ->setPrice(10)
30  ->setDescription('Description with <b>html tag</b>')
31  ->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
32  ->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
33  ->setCategoryIds([2])
34  ->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1])
35  ->save();
36 $productRepository = $objectManager->get(Magento\Catalog\Api\ProductRepositoryInterface::class);
40 $product = $objectManager->create(\Magento\Catalog\Model\Product::class);
42  ->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_BUNDLE)
43  ->setAttributeSetId(4)
44  ->setWebsiteIds([1])
45  ->setName('Bundle Product')
46  ->setSku('bundle-product')
47  ->setDescription('Description with <b>html tag</b>')
48  ->setShortDescription('Bundle')
49  ->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
50  ->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
51  ->setStockData(
52  [
53  'use_config_manage_stock' => 0,
54  'manage_stock' => 0,
55  'use_config_enable_qty_increments' => 1,
56  'use_config_qty_increments' => 1,
57  'is_in_stock' => 0,
58  ]
59  )
60  ->setBundleOptionsData(
61  [
62  [
63  'title' => 'Bundle Product Items',
64  'default_title' => 'Bundle Product Items',
65  'type' => 'checkbox',
66  'required' => 1,
67  'delete' => '',
68  'position' => 0,
69  'option_id' => '',
70  ],
71  ]
72  )
73  ->setBundleSelectionsData(
74  [
75  [
76  [
77  'product_id' => $simpleProducts[0]->getId(),
78  'selection_qty' => 1,
79  'selection_can_change_qty' => 1,
80  'delete' => '',
81  'position' => 0,
82  'selection_price_type' => 0,
83  'selection_price_value' => 0.0,
84  'option_id' => '',
85  'selection_id' => '',
86  'is_default' => 1,
87  ],
88  [
89  'product_id' => $simpleProducts[1]->getId(),
90  'selection_qty' => 1,
91  'selection_can_change_qty' => 1,
92  'delete' => '',
93  'position' => 0,
94  'selection_price_type' => 0,
95  'selection_price_value' => 0.0,
96  'option_id' => '',
97  'selection_id' => '',
98  'is_default' => 1,
99  ]
100  ],
101  ]
102  )->setCustomAttributes([
103  "price_type" => [
104  'attribute_code' => 'price_type',
105  'value' => \Magento\Bundle\Model\Product\Price::PRICE_TYPE_DYNAMIC
106  ],
107  "price_view" => [
108  "attribute_code" => "price_view",
109  "value" => "1",
110  ],
111  ])
112  ->setCanSaveBundleSelections(true)
113  ->setHasOptions(false)
114  ->setAffectBundleProductSelections(true);
115 if ($product->getBundleOptionsData()) {
116  $options = [];
117  foreach ($product->getBundleOptionsData() as $key => $optionData) {
118  if (!(bool)$optionData['delete']) {
119  $option = $objectManager->create(\Magento\Bundle\Api\Data\OptionInterfaceFactory::class)
120  ->create(['data' => $optionData]);
121  $option->setSku($product->getSku());
122  $option->setOptionId(null);
123 
124  $links = [];
125  $bundleLinks = $product->getBundleSelectionsData();
126  if (!empty($bundleLinks[$key])) {
127  foreach ($bundleLinks[$key] as $linkData) {
128  if (!(bool)$linkData['delete']) {
130  $link = $objectManager->create(\Magento\Bundle\Api\Data\LinkInterfaceFactory::class)
131  ->create(['data' => $linkData]);
132  $linkProduct = $productRepository->getById($linkData['product_id']);
133  $link->setSku($linkProduct->getSku());
134  $link->setQty($linkData['selection_qty']);
135  if (isset($linkData['selection_can_change_qty'])) {
136  $link->setCanChangeQuantity($linkData['selection_can_change_qty']);
137  }
138  $links[] = $link;
139  }
140  }
141  $option->setProductLinks($links);
142  $options[] = $option;
143  }
144  }
145  }
146  $extension = $product->getExtensionAttributes();
147  $extension->setBundleProductOptions($options);
148  $product->setExtensionAttributes($extension);
149 }
151 
152 $product = $productRepository->get($product->getSku());
156 foreach ($product->getExtensionAttributes()->getBundleProductOptions() as $option) {
157  foreach ($option->getProductLinks() as $selection) {
161  $bundleOptions[$option->getId()][] = $selection->getId();
162  $bundleOptionsQty[$option->getId()][] = 1;
163  }
164 }
165 
166 $buyRequest = new \Magento\Framework\DataObject(
167  ['qty' => 1, 'bundle_option' => $bundleOptions, 'bundle_option_qty' => $bundleOptionsQty]
168 );
169 $product->setSkipCheckRequiredOption(true);
170 
171 $addressData = include __DIR__ . '/address_data.php';
172 $billingAddress = $objectManager->create(\Magento\Quote\Model\Quote\Address::class, ['data' => $addressData]);
173 $billingAddress->setAddressType('billing');
174 
177 $shippingAddress->setId(null)->setAddressType('shipping');
178 
180 $quote = $objectManager->create(\Magento\Quote\Model\Quote::class);
181 $quote
182  ->setCustomerIsGuest(true)
183  ->setStoreId($objectManager->get(\Magento\Store\Model\StoreManagerInterface::class)->getStore()->getId())
184  ->setReservedOrderId('test01')
185  ->setBillingAddress($billingAddress)
186  ->setShippingAddress($shippingAddress)
187  ->setCustomerEmail('[email protected]')
188  ->addProduct($product, $buyRequest);
189 
191 $rate = $objectManager->create(\Magento\Quote\Model\Quote\Address\Rate::class);
192 $rate
193  ->setCode('freeshipping_freeshipping')
194  ->getPrice(1);
195 
196 $quote->getShippingAddress()->setShippingMethod('freeshipping_freeshipping');
197 $quote->getShippingAddress()->addShippingRate($rate);
198 $quote->getPayment()->setMethod('checkmo');
199 $quote->collectTotals();
200 $quote->save();
201 
203 $quoteIdMask = $objectManager->create(\Magento\Quote\Model\QuoteIdMaskFactory::class)->create();
204 $quoteIdMask->setQuoteId($quote->getId());
205 $quoteIdMask->setDataChanges(true);
206 $quoteIdMask->save();
$productRepository
$optionData
$bundleOptions
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60
$bundleOptionsQty
foreach($product->getExtensionAttributes() ->getBundleProductOptions() as $option) $buyRequest
$shippingAddress
$billingAddress
$simpleProducts[]
$objectManager