Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
products_multi_option.php
Go to the documentation of this file.
1 <?php
9  ->create(\Magento\Catalog\Api\Data\ProductCustomOptionInterfaceFactory::class);
12  ->create(\Magento\Catalog\Model\Product::class);
13 $product->isObjectNew(true);
14 $product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
15  ->setId(1)
16  ->setAttributeSetId(4)
17  ->setWebsiteIds([1])
18  ->setName('Unisex green socks')
19  ->setSku('green_socks')
20  ->setPrice(12)
21  ->setWeight(1)
22  ->setShortDescription("Unisex green socks for some good peoples at one")
23  ->setTaxClassId(0)
24  ->setTierPrice(
25  [
26  [
27  'website_id' => 0,
28  'cust_group' => \Magento\Customer\Model\GroupManagement::CUST_GROUP_ALL,
29  'price_qty' => 2,
30  'price' => 8,
31  ],
32  [
33  'website_id' => 0,
34  'cust_group' => \Magento\Customer\Model\GroupManagement::CUST_GROUP_ALL,
35  'price_qty' => 5,
36  'price' => 5,
37  ],
38  [
39  'website_id' => 0,
40  'cust_group' => \Magento\Customer\Model\GroupManagement::NOT_LOGGED_IN_ID,
41  'price_qty' => 3,
42  'price' => 5,
43  ],
44  ]
45  )
46  ->setDescription('Unisex <b>green socks</b> for some good peoples at one')
47  ->setMetaTitle('green socks metadata')
48  ->setMetaKeyword('green,socks,unisex')
49  ->setMetaDescription('green socks metadata description')
50  ->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
51  ->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
52  ->setCategoryIds([2])
53  ->setStockData(
54  [
55  'use_config_manage_stock' => 1,
56  'qty' => 100,
57  'is_qty_decimal' => 0,
58  'is_in_stock' => 1,
59  ]
60  );
62  'previous_group' => 'text',
63  'title' => 'Stone',
64  'type' => 'field',
65  'is_require' => 1,
66  'sort_order' => 0,
67  'price' => 1,
68  'price_type' => 'fixed',
69  'sku' => 'stone-1',
70  'max_characters' => 100,
71 ];
74 $customOption->setProductSku($product->getSku());
75 $product->setCanSaveCustomOptions(true)
76  ->setOptions([$customOption])
77  ->setHasOptions(true)
78  ->save();
79 
82  ->create(\Magento\Catalog\Model\Product::class);
83 $product->isObjectNew(true);
84 $product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
85  ->setId(2)
86  ->setAttributeSetId(4)
87  ->setWebsiteIds([1])
88  ->setName('White shorts')
89  ->setSku('white_shorts')
90  ->setPrice(14)
91  ->setWeight(2)
92  ->setShortDescription("Small white shorts for your children")
93  ->setTaxClassId(0)
94  ->setTierPrice(
95  [
96  [
97  'website_id' => 0,
98  'cust_group' => \Magento\Customer\Model\GroupManagement::CUST_GROUP_ALL,
99  'price_qty' => 2,
100  'price' => 8,
101  ],
102  [
103  'website_id' => 0,
104  'cust_group' => \Magento\Customer\Model\GroupManagement::CUST_GROUP_ALL,
105  'price_qty' => 5,
106  'price' => 5,
107  ],
108  [
109  'website_id' => 0,
110  'cust_group' => \Magento\Customer\Model\GroupManagement::NOT_LOGGED_IN_ID,
111  'price_qty' => 3,
112  'price' => 5,
113  ],
114  ]
115  )
116  ->setDescription('Small <b>white shorts</b> for your children')
117  ->setMetaTitle('white shorts for your children metadata')
118  ->setMetaKeyword('white,shorts,children')
119  ->setMetaDescription('white shorts for your children metadata description')
120  ->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
121  ->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
122  ->setCategoryIds([2])
123  ->setStockData(
124  [
125  'use_config_manage_stock' => 1,
126  'qty' => 100,
127  'is_qty_decimal' => 0,
128  'is_in_stock' => 1,
129  ]
130  );
131 $option = [
132  'previous_group' => 'text',
133  'title' => 'Gold',
134  'type' => 'field',
135  'is_require' => 1,
136  'sort_order' => 0,
137  'price' => 1,
138  'price_type' => 'fixed',
139  'sku' => 'Gold',
140  'max_characters' => 100,
141 ];
143 $customOption = $customOptionFactory->create(['data' => $option]);
144 $customOption->setProductSku($product->getSku());
145 $product->setCanSaveCustomOptions(true)
146  ->setOptions([$customOption])
147  ->setHasOptions(true)
148  ->save();
149 
152  ->create(\Magento\Catalog\Model\Product::class);
153 $product->isObjectNew(true);
154 $product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
155  ->setId(3)
156  ->setAttributeSetId(4)
157  ->setWebsiteIds([1])
158  ->setName('Red trousers')
159  ->setSku('red_trousers')
160  ->setPrice(16)
161  ->setWeight(3)
162  ->setShortDescription("Red pants for men")
163  ->setTaxClassId(0)
164  ->setTierPrice(
165  [
166  [
167  'website_id' => 0,
168  'cust_group' => \Magento\Customer\Model\GroupManagement::CUST_GROUP_ALL,
169  'price_qty' => 2,
170  'price' => 8,
171  ],
172  [
173  'website_id' => 0,
174  'cust_group' => \Magento\Customer\Model\GroupManagement::CUST_GROUP_ALL,
175  'price_qty' => 5,
176  'price' => 5,
177  ],
178  [
179  'website_id' => 0,
180  'cust_group' => \Magento\Customer\Model\GroupManagement::NOT_LOGGED_IN_ID,
181  'price_qty' => 3,
182  'price' => 5,
183  ],
184  ]
185  )
186  ->setDescription('Red pants for <b>men</b>')
187  ->setMetaTitle('Red trousers meta title')
188  ->setMetaKeyword('red,trousers,meta,men')
189  ->setMetaDescription('Red trousers meta description')
190  ->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
191  ->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
192  ->setCategoryIds([2])
193  ->setStockData(
194  [
195  'use_config_manage_stock' => 1,
196  'qty' => 100,
197  'is_qty_decimal' => 0,
198  'is_in_stock' => 1,
199  ]
200  );
201 $option = [
202  'previous_group' => 'text',
203  'title' => 'Silver',
204  'type' => 'field',
205  'is_require' => 1,
206  'sort_order' => 0,
207  'price' => 1,
208  'price_type' => 'fixed',
209  'sku' => 'silver',
210  'max_characters' => 100,
211 ];
213 $customOption = $customOptionFactory->create(['data' => $option]);
214 $customOption->setProductSku($product->getSku());
215 $product->setCanSaveCustomOptions(true)
216  ->setOptions([$customOption])
217  ->setHasOptions(true)
218  ->save();
219 
222  ->create(\Magento\Catalog\Model\Product::class);
223 $product->isObjectNew(true);
224 $product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
225  ->setId(4)
226  ->setAttributeSetId(4)
227  ->setWebsiteIds([1])
228  ->setName('Blue briefs')
229  ->setSku('blue_briefs')
230  ->setPrice(18)
231  ->setWeight(3)
232  ->setShortDescription("Blue briefs for Russian men")
233  ->setTaxClassId(0)
234  ->setTierPrice(
235  [
236  [
237  'website_id' => 0,
238  'cust_group' => \Magento\Customer\Model\GroupManagement::CUST_GROUP_ALL,
239  'price_qty' => 2,
240  'price' => 8,
241  ],
242  [
243  'website_id' => 0,
244  'cust_group' => \Magento\Customer\Model\GroupManagement::CUST_GROUP_ALL,
245  'price_qty' => 5,
246  'price' => 5,
247  ],
248  [
249  'website_id' => 0,
250  'cust_group' => \Magento\Customer\Model\GroupManagement::NOT_LOGGED_IN_ID,
251  'price_qty' => 3,
252  'price' => 5,
253  ],
254  ]
255  )
256  ->setDescription('Blue briefs for <b>men</b>')
257  ->setMetaTitle('Blue briefs meta title')
258  ->setMetaKeyword('blue,briefs,meta,men')
259  ->setMetaDescription('Blue briefs meta description')
260  ->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
261  ->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
262  ->setCategoryIds([2])
263  ->setStockData(
264  [
265  'use_config_manage_stock' => 1,
266  'qty' => 100,
267  'is_qty_decimal' => 0,
268  'is_in_stock' => 1,
269  ]
270  )
271  ->save();
272 
275  ->create(\Magento\Catalog\Model\Product::class);
276 $product->isObjectNew(true);
277 $product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
278  ->setId(5)
279  ->setAttributeSetId(4)
280  ->setWebsiteIds([1])
281  ->setName('Grey shorts')
282  ->setSku('grey_shorts')
283  ->setPrice(20)
284  ->setWeight(3)
285  ->setShortDescription("Grey or green shorts for all peoples at one")
286  ->setTaxClassId(0)
287  ->setTierPrice(
288  [
289  [
290  'website_id' => 0,
291  'cust_group' => \Magento\Customer\Model\GroupManagement::CUST_GROUP_ALL,
292  'price_qty' => 2,
293  'price' => 8,
294  ],
295  [
296  'website_id' => 0,
297  'cust_group' => \Magento\Customer\Model\GroupManagement::CUST_GROUP_ALL,
298  'price_qty' => 5,
299  'price' => 5,
300  ],
301  [
302  'website_id' => 0,
303  'cust_group' => \Magento\Customer\Model\GroupManagement::NOT_LOGGED_IN_ID,
304  'price_qty' => 3,
305  'price' => 5,
306  ],
307  ]
308  )
309  ->setDescription('Grey or green shorts for peoples at <b>one</b>')
310  ->setMetaTitle('Grey shorts meta title')
311  ->setMetaKeyword('grey,shorts,meta,men')
312  ->setMetaDescription('Grey shorts meta description')
313  ->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
314  ->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
315  ->setCategoryIds([2])
316  ->setStockData(
317  [
318  'use_config_manage_stock' => 1,
319  'qty' => 100,
320  'is_qty_decimal' => 0,
321  'is_in_stock' => 1,
322  ]
323  )
324  ->save();
325 
328  ->create(\Magento\Catalog\Model\Product::class);
329 $product->isObjectNew(true);
330 $product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
331  ->setId(6)
332  ->setAttributeSetId(4)
333  ->setWebsiteIds([1])
334  ->setName('Blue shorts')
335  ->setSku('blue_shorts')
336  ->setPrice(22)
337  ->setWeight(3)
338  ->setShortDescription("Blue or green shorts for all peoples at one")
339  ->setTaxClassId(0)
340  ->setTierPrice(
341  [
342  [
343  'website_id' => 0,
344  'cust_group' => \Magento\Customer\Model\GroupManagement::CUST_GROUP_ALL,
345  'price_qty' => 2,
346  'price' => 8,
347  ],
348  [
349  'website_id' => 0,
350  'cust_group' => \Magento\Customer\Model\GroupManagement::CUST_GROUP_ALL,
351  'price_qty' => 5,
352  'price' => 5,
353  ],
354  [
355  'website_id' => 0,
356  'cust_group' => \Magento\Customer\Model\GroupManagement::NOT_LOGGED_IN_ID,
357  'price_qty' => 3,
358  'price' => 5,
359  ],
360  ]
361  )
362  ->setDescription('Blue or green shorts for peoples at <b>one</b>')
363  ->setMetaTitle('blue shorts meta title')
364  ->setMetaKeyword('blue,shorts,meta,men')
365  ->setMetaDescription('blue shorts meta description')
366  ->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
367  ->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
368  ->setCategoryIds([2])
369  ->setStockData(
370  [
371  'use_config_manage_stock' => 1,
372  'qty' => 100,
373  'is_qty_decimal' => 0,
374  'is_in_stock' => 1,
375  ]
376  )
377  ->save();
378 
381  ->create(\Magento\Catalog\Model\Product::class);
382 $product->isObjectNew(true);
383 $product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
384  ->setId(7)
385  ->setAttributeSetId(4)
386  ->setWebsiteIds([1])
387  ->setName('Red shorts')
388  ->setSku('red_shorts')
389  ->setPrice(24)
390  ->setWeight(3)
391  ->setShortDescription("Red or green shorts for all peoples at one")
392  ->setTaxClassId(0)
393  ->setTierPrice(
394  [
395  [
396  'website_id' => 0,
397  'cust_group' => \Magento\Customer\Model\GroupManagement::CUST_GROUP_ALL,
398  'price_qty' => 2,
399  'price' => 8,
400  ],
401  [
402  'website_id' => 0,
403  'cust_group' => \Magento\Customer\Model\GroupManagement::CUST_GROUP_ALL,
404  'price_qty' => 5,
405  'price' => 5,
406  ],
407  [
408  'website_id' => 0,
409  'cust_group' => \Magento\Customer\Model\GroupManagement::NOT_LOGGED_IN_ID,
410  'price_qty' => 3,
411  'price' => 5,
412  ],
413  ]
414  )
415  ->setDescription('Red or green shorts for peoples at <b>one</b>')
416  ->setMetaTitle('red shorts meta title')
417  ->setMetaKeyword('red,shorts,meta,men')
418  ->setMetaDescription('red shorts meta description')
419  ->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
420  ->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
421  ->setCategoryIds([2])
422  ->setStockData(
423  [
424  'use_config_manage_stock' => 1,
425  'qty' => 100,
426  'is_qty_decimal' => 0,
427  'is_in_stock' => 1,
428  ]
429  )
430  ->save();
431 
434  ->create(\Magento\Catalog\Model\Product::class);
435 $product->isObjectNew(true);
436 $product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
437  ->setId(8)
438  ->setAttributeSetId(4)
439  ->setWebsiteIds([1])
440  ->setName('Yellow shorts')
441  ->setSku('yellow_shorts')
442  ->setPrice(26)
443  ->setWeight(3)
444  ->setShortDescription("Yellow or green shorts for all peoples at one")
445  ->setTaxClassId(0)
446  ->setTierPrice(
447  [
448  [
449  'website_id' => 0,
450  'cust_group' => \Magento\Customer\Model\GroupManagement::CUST_GROUP_ALL,
451  'price_qty' => 2,
452  'price' => 8,
453  ],
454  [
455  'website_id' => 0,
456  'cust_group' => \Magento\Customer\Model\GroupManagement::CUST_GROUP_ALL,
457  'price_qty' => 5,
458  'price' => 5,
459  ],
460  [
461  'website_id' => 0,
462  'cust_group' => \Magento\Customer\Model\GroupManagement::NOT_LOGGED_IN_ID,
463  'price_qty' => 3,
464  'price' => 5,
465  ],
466  ]
467  )
468  ->setDescription('Yellow or green shorts for peoples at <b>one</b>')
469  ->setMetaTitle('yellow shorts meta title')
470  ->setMetaKeyword('yellow,shorts,meta,men')
471  ->setMetaDescription('yellow shorts meta description')
472  ->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
473  ->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
474  ->setCategoryIds([2])
475  ->setStockData(
476  [
477  'use_config_manage_stock' => 1,
478  'qty' => 100,
479  'is_qty_decimal' => 0,
480  'is_in_stock' => 1,
481  ]
482  )
483  ->save();
$customOptionFactory