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