Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
product_with_options.php
Go to the documentation of this file.
1 <?php
8 
10 $product = $objectManager->create(\Magento\Catalog\Model\Product::class);
11 
12 $product->setTypeId(
13  'simple'
14 )->setAttributeSetId(
15  4
16 )->setWebsiteIds(
17  [1]
18 )->setName(
19  'Simple Product With Custom Options'
20 )->setSku(
21  'simple'
22 )->setPrice(
23  10
24 )->setMetaTitle(
25  'meta title'
26 )->setMetaKeyword(
27  'meta keyword'
28 )->setMetaDescription(
29  'meta description'
30 )->setVisibility(
31  \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH
32 )->setStatus(
33  \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED
34 )->setCanSaveCustomOptions(
35  true
36 )->setStockData(
37  [
38  'qty' => 100,
39  'is_in_stock' => 1,
40  'manage_stock' => 1,
41  ]
42 )->setHasOptions(true);
43 
45  [
46  'title' => 'test_option_code_1',
47  'type' => 'field',
48  'is_require' => true,
49  'sort_order' => 1,
50  'price' => -10.0,
51  'price_type' => 'fixed',
52  'sku' => 'sku1',
53  'max_characters' => 10,
54  ],
55  [
56  'title' => 'area option',
57  'type' => 'area',
58  'is_require' => false,
59  'sort_order' => 2,
60  'price' => 20.0,
61  'price_type' => 'percent',
62  'sku' => 'sku2',
63  'max_characters' => 20
64  ],
65  [
66  'title' => 'file option',
67  'type' => 'file',
68  'is_require' => true,
69  'sort_order' => 3,
70  'price' => 30.0,
71  'price_type' => 'percent',
72  'sku' => 'sku3',
73  'file_extension' => 'jpg, png, gif',
74  'image_size_x' => 10,
75  'image_size_y' => 20
76 
77  ],
78  [
79  'title' => 'drop_down option',
80  'type' => 'drop_down',
81  'is_require' => true,
82  'sort_order' => 4,
83  'values' => [
84  [
85  'title' => 'drop_down option 1',
86  'price' => 10,
87  'price_type' => 'fixed',
88  'sku' => 'drop_down option 1 sku',
89  'sort_order' => 1,
90  ],
91  [
92  'title' => 'drop_down option 2',
93  'price' => 20,
94  'price_type' => 'fixed',
95  'sku' => 'drop_down option 2 sku',
96  'sort_order' => 2,
97  ],
98  ],
99  ],
100  [
101  'title' => 'radio option',
102  'type' => 'radio',
103  'is_require' => true,
104  'sort_order' => 5,
105  'values' => [
106  [
107  'title' => 'radio option 1',
108  'price' => 10,
109  'price_type' => 'fixed',
110  'sku' => 'radio option 1 sku',
111  'sort_order' => 1,
112  ],
113  [
114  'title' => 'radio option 2',
115  'price' => 20,
116  'price_type' => 'fixed',
117  'sku' => 'radio option 2 sku',
118  'sort_order' => 2,
119  ],
120  ],
121  ],
122  [
123  'title' => 'checkbox option',
124  'type' => 'checkbox',
125  'is_require' => true,
126  'sort_order' => 6,
127  'values' => [
128  [
129  'title' => 'checkbox option 1',
130  'price' => 10,
131  'price_type' => 'fixed',
132  'sku' => 'checkbox option 1 sku',
133  'sort_order' => 1,
134  ],
135  [
136  'title' => 'checkbox option 2',
137  'price' => 20,
138  'price_type' => 'fixed',
139  'sku' => 'checkbox option 2 sku',
140  'sort_order' => 2,
141  ],
142  ],
143  ],
144  [
145  'title' => 'multiple option',
146  'type' => 'multiple',
147  'is_require' => true,
148  'sort_order' => 7,
149  'values' => [
150  [
151  'title' => 'multiple option 1',
152  'price' => 10,
153  'price_type' => 'fixed',
154  'sku' => 'multiple option 1 sku',
155  'sort_order' => 1,
156  ],
157  [
158  'title' => 'multiple option 2',
159  'price' => 20,
160  'price_type' => 'fixed',
161  'sku' => 'multiple option 2 sku',
162  'sort_order' => 2,
163  ],
164  ],
165  ],
166  [
167  'title' => 'date option',
168  'type' => 'date',
169  'price' => 80.0,
170  'price_type' => 'fixed',
171  'sku' => 'date option sku',
172  'is_require' => true,
173  'sort_order' => 8
174  ],
175  [
176  'title' => 'date_time option',
177  'type' => 'date_time',
178  'price' => 90.0,
179  'price_type' => 'fixed',
180  'is_require' => true,
181  'sort_order' => 9,
182  'sku' => 'date_time option sku'
183  ],
184  [
185  'title' => 'time option',
186  'type' => 'time',
187  'price' => 100.0,
188  'price_type' => 'fixed',
189  'is_require' => true,
190  'sku' => 'time option sku',
191  'sort_order' => 10
192  ]
193 ];
194 
196 
198 $customOptionFactory = $objectManager->create(\Magento\Catalog\Api\Data\ProductCustomOptionInterfaceFactory::class);
199 
200 foreach ($options as $option) {
202  $customOption = $customOptionFactory->create(['data' => $option]);
203  $customOption->setProductSku($product->getSku());
204 
206 }
207 
208 $product->setOptions($customOptions);
209 
211 $productRepository = $objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class);
$customOption
Definition: products.php:73
$customOptionFactory