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