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