Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
product_simple_with_full_option_set.php
Go to the documentation of this file.
1 <?php
7 use Magento\Catalog\Api\Data\ProductTierPriceExtensionFactory;
8 
10 
13 
15 $categoryLinkManagement = $objectManager->get(\Magento\Catalog\Api\CategoryLinkManagementInterface::class);
16 
19 $tierPriceFactory = $objectManager->get(\Magento\Catalog\Api\Data\ProductTierPriceInterfaceFactory::class);
21 $tpExtensionAttributesFactory = $objectManager->get(ProductTierPriceExtensionFactory::class);
22 
23 $adminWebsite = $objectManager->get(\Magento\Store\Api\WebsiteRepositoryInterface::class)->get('admin');
25  ->setWebsiteId($adminWebsite->getId());
26 
28  [
29  'data' => [
30  'customer_group_id' => \Magento\Customer\Model\Group::CUST_GROUP_ALL,
31  'qty' => 2,
32  'value' => 8
33  ]
34  ]
35 )->setExtensionAttributes($tierPriceExtensionAttributes1);
36 
37 $tierPrices[] = $tierPriceFactory->create(
38  [
39  'data' => [
40  'customer_group_id' => \Magento\Customer\Model\Group::CUST_GROUP_ALL,
41  'qty' => 5,
42  'value' => 5
43  ]
44  ]
45 )->setExtensionAttributes($tierPriceExtensionAttributes1);
46 
47 $tierPrices[] = $tierPriceFactory->create(
48  [
49  'data' => [
50  'customer_group_id' => \Magento\Customer\Model\Group::NOT_LOGGED_IN_ID,
51  'qty' => 3,
52  'value' => 5
53  ]
54  ]
55 )->setExtensionAttributes($tierPriceExtensionAttributes1);
56 
58  ->setWebsiteId($adminWebsite->getId())
59  ->setPercentageValue(50);
60 
61 $tierPrices[] = $tierPriceFactory->create(
62  [
63  'data' => [
64  'customer_group_id' => \Magento\Customer\Model\Group::NOT_LOGGED_IN_ID,
65  'qty' => 10
66  ]
67  ]
68 )->setExtensionAttributes($tierPriceExtensionAttributes2);
69 
71 $product = $objectManager->create(\Magento\Catalog\Model\Product::class);
72 $product->isObjectNew(true);
73 $product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
74  ->setId(1)
75  ->setAttributeSetId(4)
76  ->setWebsiteIds([1])
77  ->setName('Simple Product')
78  ->setSku('simple')
79  ->setPrice(10)
80  ->setWeight(1)
81  ->setShortDescription("Short description")
82  ->setTaxClassId(0)
83  ->setTierPrices($tierPrices)
84  ->setDescription('Description with <b>html tag</b>')
85  ->setMetaTitle('meta title')
86  ->setMetaKeyword('meta keyword')
87  ->setMetaDescription('meta description')
88  ->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
89  ->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
90  ->setStockData(
91  [
92  'use_config_manage_stock' => 1,
93  'qty' => 100,
94  'is_qty_decimal' => 0,
95  'is_in_stock' => 1,
96  ]
97  )->setCanSaveCustomOptions(true)
98  ->setHasOptions(true);
99 
101  [
102  'previous_group' => 'text',
103  'title' => 'Test Field',
104  'type' => 'field',
105  'is_require' => 1,
106  'sort_order' => 0,
107  'price' => 1,
108  'price_type' => 'fixed',
109  'sku' => '1-text',
110  'max_characters' => 100,
111  ],
112  [
113  'previous_group' => 'date',
114  'title' => 'Test Date and Time',
115  'type' => 'date_time',
116  'is_require' => 1,
117  'sort_order' => 0,
118  'price' => 2,
119  'price_type' => 'fixed',
120  'sku' => '2-date',
121  ],
122  [
123  'previous_group' => 'select',
124  'title' => 'Test Select',
125  'type' => 'drop_down',
126  'is_require' => 1,
127  'sort_order' => 0,
128  'values' => [
129  [
130  'option_type_id' => null,
131  'title' => 'Option 1',
132  'price' => 3,
133  'price_type' => 'fixed',
134  'sku' => '3-1-select',
135  ],
136  [
137  'option_type_id' => null,
138  'title' => 'Option 2',
139  'price' => 3,
140  'price_type' => 'fixed',
141  'sku' => '3-2-select',
142  ],
143  ]
144  ],
145  [
146  'previous_group' => 'select',
147  'title' => 'Test Radio',
148  'type' => 'radio',
149  'is_require' => 1,
150  'sort_order' => 0,
151  'values' => [
152  [
153  'option_type_id' => null,
154  'title' => 'Option 1',
155  'price' => 3,
156  'price_type' => 'fixed',
157  'sku' => '4-1-radio',
158  ],
159  [
160  'option_type_id' => null,
161  'title' => 'Option 2',
162  'price' => 3,
163  'price_type' => 'fixed',
164  'sku' => '4-2-radio',
165  ],
166  ]
167  ],
168  [
169  'previous_group' => 'select',
170  'title' => 'File option',
171  'type' => 'file',
172  'sort_order' => 3,
173  'is_require' => 1,
174  'price' => 30,
175  'price_type' => 'percent',
176  'sku' => 'sku3',
177  'file_extension' => 'jpg, png, gif',
178  'image_size_x' => 10,
179  'image_size_y' => 20,
180  ],
181  [
182  'title' => 'area option',
183  'type' => 'area',
184  'sort_order' => 2,
185  'is_require' => 1,
186  'price' => 3.95,
187  'price_type' => 'percent',
188  'sku' => 'sku2',
189  'max_characters' => 20,
190  ]
191 ];
192 
193 $options = [];
194 
196 $customOptionFactory = $objectManager->create(\Magento\Catalog\Api\Data\ProductCustomOptionInterfaceFactory::class);
197 
198 foreach ($oldOptions as $option) {
200  $option = $customOptionFactory->create(['data' => $option]);
201  $option->setProductSku($product->getSku());
202 
203  $options[] = $option;
204 }
205 
206 $product->setOptions($options);
207 
209 $productRepository = $objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class);
211 
212 $categoryLinkManagement->assignProductToCategories(
213  $product->getSku(),
214  [2]
215 );