Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
product_with_multiple_options.php
Go to the documentation of this file.
1 <?php
7 require __DIR__ . '/../../../Magento/Catalog/_files/multiple_products.php';
8 
10 
11 $productIds = range(10, 12, 1);
12 foreach ($productIds as $productId) {
14  $stockItem = $objectManager->create(\Magento\CatalogInventory\Model\Stock\Item::class);
15  $stockItem->load($productId, 'product_id');
16 
17  if (!$stockItem->getProductId()) {
18  $stockItem->setProductId($productId);
19  }
20  $stockItem->setUseConfigManageStock(1);
21  $stockItem->setQty(1000);
22  $stockItem->setIsQtyDecimal(0);
23  $stockItem->setIsInStock(1);
24  $stockItem->save();
25 }
26 
28 $product = $objectManager->create(\Magento\Catalog\Model\Product::class);
29 $product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_BUNDLE)
30  ->setId(3)
31  ->setAttributeSetId(4)
32  ->setWebsiteIds([1])
33  ->setName('Bundle Product')
34  ->setSku('bundle-product')
35  ->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
36  ->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
37  ->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1])
38  ->setPriceView(1)
39  ->setPriceType(1)
40  ->setPrice(10.0)
41  ->setShipmentType(0)
42  ->setBundleOptionsData(
43  [
44  // Required "Drop-down" option
45  [
46  'title' => 'Option 1',
47  'default_title' => 'Option 1',
48  'type' => 'select',
49  'required' => 1,
50  'position' => 1,
51  'delete' => '',
52  ],
53  // Required "Radio Buttons" option
54  [
55  'title' => 'Option 2',
56  'default_title' => 'Option 2',
57  'type' => 'radio',
58  'required' => 1,
59  'position' => 2,
60  'delete' => '',
61  ],
62  // Required "Checkbox" option
63  [
64  'title' => 'Option 3',
65  'default_title' => 'Option 3',
66  'type' => 'checkbox',
67  'required' => 1,
68  'position' => 3,
69  'delete' => '',
70  ],
71  // Required "Multiple Select" option
72  [
73  'title' => 'Option 4',
74  'default_title' => 'Option 4',
75  'type' => 'multi',
76  'required' => 1,
77  'position' => 4,
78  'delete' => '',
79  ],
80  // Non-required "Multiple Select" option
81  [
82  'title' => 'Option 5',
83  'default_title' => 'Option 5',
84  'type' => 'multi',
85  'required' => 0,
86  'position' => 5,
87  'delete' => '',
88  ]
89  ]
90  )->setBundleSelectionsData(
91  [
92  [
93  [
94  'product_id' => 10,
95  'selection_qty' => 1,
96  'selection_can_change_qty' => 1,
97  'delete' => '',
98  'option_id' => 1
99  ],
100  [
101  'product_id' => 11,
102  'selection_qty' => 1,
103  'selection_can_change_qty' => 1,
104  'delete' => '',
105  'option_id' => 1
106  ]
107  ],
108  [
109  [
110  'product_id' => 10,
111  'selection_qty' => 1,
112  'selection_can_change_qty' => 1,
113  'delete' => '',
114  'option_id' => 2
115  ],
116  [
117  'product_id' => 11,
118  'selection_qty' => 1,
119  'selection_can_change_qty' => 1,
120  'delete' => '',
121  'option_id' => 2
122  ]
123  ],
124  [
125  [
126  'product_id' => 10,
127  'selection_qty' => 1,
128  'delete' => '',
129  'option_id' => 3
130  ],
131  [
132  'product_id' => 11,
133  'selection_qty' => 1,
134  'delete' => '',
135  'option_id' => 3
136  ]
137  ],
138  [
139  [
140  'product_id' => 10,
141  'selection_qty' => 1,
142  'delete' => '',
143  'option_id' => 4
144  ],
145  [
146  'product_id' => 11,
147  'selection_qty' => 1,
148  'delete' => '',
149  'option_id' => 4
150  ]
151  ],
152  [
153  [
154  'product_id' => 10,
155  'selection_qty' => 1,
156  'delete' => '',
157  'option_id' => 5
158  ],
159  [
160  'product_id' => 11,
161  'selection_qty' => 1,
162  'delete' => '',
163  'option_id' => 5
164  ]
165  ]
166  ]
167  );
168 $productRepository = $objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class);
169 
170 if ($product->getBundleOptionsData()) {
171  $options = [];
172  foreach ($product->getBundleOptionsData() as $key => $optionData) {
173  if (!(bool)$optionData['delete']) {
174  $option = $objectManager->create(\Magento\Bundle\Api\Data\OptionInterfaceFactory::class)
175  ->create(['data' => $optionData]);
176  $option->setSku($product->getSku());
177  $option->setOptionId(null);
178 
179  $links = [];
180  $bundleLinks = $product->getBundleSelectionsData();
181  if (!empty($bundleLinks[$key])) {
182  foreach ($bundleLinks[$key] as $linkData) {
183  if (!(bool)$linkData['delete']) {
184  $link = $objectManager->create(\Magento\Bundle\Api\Data\LinkInterfaceFactory::class)
185  ->create(['data' => $linkData]);
186  $linkProduct = $productRepository->getById($linkData['product_id']);
187  $link->setSku($linkProduct->getSku());
188  $link->setQty($linkData['selection_qty']);
189  $links[] = $link;
190  }
191  }
192  $option->setProductLinks($links);
193  $options[] = $option;
194  }
195  }
196  }
197  $extension = $product->getExtensionAttributes();
198  $extension->setBundleProductOptions($options);
199  $product->setExtensionAttributes($extension);
200 }
201 $product->save();
$optionData
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60