Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
product_simple_with_admin_store.php
Go to the documentation of this file.
1 <?php
8 
11 
13 $storeManager = $objectManager->get(\Magento\Store\Model\StoreManagerInterface::class);
14 $store = $storeManager->getStore(\Magento\Store\Model\Store::ADMIN_CODE);
15 $storeManager->setCurrentStore($store->getCode());
16 
18 $categoryLinkManagement = $objectManager->create(\Magento\Catalog\Api\CategoryLinkManagementInterface::class);
19 
21 $product = $objectManager->create(\Magento\Catalog\Model\Product::class);
22 $product->isObjectNew(true);
23 $product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
24  ->setId(1)
25  ->setAttributeSetId(4)
26  ->setWebsiteIds([1])
27  ->setName('Simple Product')
28  ->setSku('simple')
29  ->setPrice(10)
30  ->setWeight(1)
31  ->setShortDescription("Short description")
32  ->setTaxClassId(0)
33  ->setTierPrice(
34  [
35  [
36  'website_id' => 0,
37  'cust_group' => \Magento\Customer\Model\Group::CUST_GROUP_ALL,
38  'price_qty' => 2,
39  'price' => 8,
40  ],
41  [
42  'website_id' => 0,
43  'cust_group' => \Magento\Customer\Model\Group::CUST_GROUP_ALL,
44  'price_qty' => 5,
45  'price' => 5,
46  ],
47  [
48  'website_id' => 0,
49  'cust_group' => \Magento\Customer\Model\Group::NOT_LOGGED_IN_ID,
50  'price_qty' => 3,
51  'price' => 5,
52  ],
53  ]
54  )
55  ->setDescription('Description with <b>html tag</b>')
56  ->setMetaTitle('meta title')
57  ->setMetaKeyword('meta keyword')
58  ->setMetaDescription('meta description')
59  ->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
60  ->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
61  ->setStockData(
62  [
63  'use_config_manage_stock' => 1,
64  'qty' => 100,
65  'is_qty_decimal' => 0,
66  'is_in_stock' => 1,
67  ]
68  )->setCanSaveCustomOptions(true)
69  ->setHasOptions(true);
70 
72  [
73  'previous_group' => 'text',
74  'title' => 'Test Field',
75  'type' => 'field',
76  'is_require' => 1,
77  'sort_order' => 0,
78  'price' => 1,
79  'price_type' => 'fixed',
80  'sku' => '1-text',
81  'max_characters' => 100,
82  ],
83  [
84  'previous_group' => 'date',
85  'title' => 'Test Date and Time',
86  'type' => 'date_time',
87  'is_require' => 1,
88  'sort_order' => 0,
89  'price' => 2,
90  'price_type' => 'fixed',
91  'sku' => '2-date',
92  ],
93  [
94  'previous_group' => 'select',
95  'title' => 'Test Select',
96  'type' => 'drop_down',
97  'is_require' => 1,
98  'sort_order' => 0,
99  'values' => [
100  [
101  'option_type_id' => null,
102  'title' => 'Option 1',
103  'price' => 3,
104  'price_type' => 'fixed',
105  'sku' => '3-1-select',
106  ],
107  [
108  'option_type_id' => null,
109  'title' => 'Option 2',
110  'price' => 3,
111  'price_type' => 'fixed',
112  'sku' => '3-2-select',
113  ],
114  ]
115  ],
116  [
117  'previous_group' => 'select',
118  'title' => 'Test Radio',
119  'type' => 'radio',
120  'is_require' => 1,
121  'sort_order' => 0,
122  'values' => [
123  [
124  'option_type_id' => null,
125  'title' => 'Option 1',
126  'price' => 3,
127  'price_type' => 'fixed',
128  'sku' => '4-1-radio',
129  ],
130  [
131  'option_type_id' => null,
132  'title' => 'Option 2',
133  'price' => 3,
134  'price_type' => 'fixed',
135  'sku' => '4-2-radio',
136  ],
137  ]
138  ]
139 ];
140 
141 $options = [];
142 
144 $customOptionFactory = $objectManager->create(\Magento\Catalog\Api\Data\ProductCustomOptionInterfaceFactory::class);
145 
146 foreach ($oldOptions as $option) {
148  $option = $customOptionFactory->create(['data' => $option]);
149  $option->setProductSku($product->getSku());
150 
151  $options[] = $option;
152 }
153 
154 $product->setOptions($options);
155 
157 $productRepositoryFactory = $objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class);
159 
160 $categoryLinkManagement->assignProductToCategories(
161  $product->getSku(),
162  [2]
163 );