Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
product_simple_with_custom_options.php
Go to the documentation of this file.
1 <?php
8 
11 
13 $categoryLinkManagement = $objectManager->create(\Magento\Catalog\Api\CategoryLinkManagementInterface::class);
14 
16 $product = $objectManager->create(\Magento\Catalog\Model\Product::class);
17 $product->isObjectNew(true);
18 $product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
19  ->setAttributeSetId(4)
20  ->setWebsiteIds([1])
21  ->setName('Simple Product')
22  ->setSku('simple_with_custom_options')
23  ->setPrice(10)
24  ->setWeight(1)
25  ->setShortDescription("Short description")
26  ->setTaxClassId(0)
27  ->setDescription('Description with <b>html tag</b>')
28  ->setMetaTitle('meta title')
29  ->setMetaKeyword('meta keyword')
30  ->setMetaDescription('meta description')
31  ->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
32  ->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
33  ->setStockData(
34  [
35  'use_config_manage_stock' => 1,
36  'qty' => 100,
37  'is_qty_decimal' => 0,
38  'is_in_stock' => 1,
39  ]
40  )->setCanSaveCustomOptions(true)
41  ->setHasOptions(true);
42 
44  [
45  'previous_group' => 'select',
46  'title' => 'Test Select',
47  'type' => 'drop_down',
48  'is_require' => 1,
49  'sort_order' => 0,
50  'values' => [
51  [
52  'option_type_id' => null,
53  'title' => 'Option 1',
54  'price' => '-3,000.00',
55  'price_type' => 'fixed',
56  'sku' => '3-1-select',
57  ],
58  [
59  'option_type_id' => null,
60  'title' => 'Option 2',
61  'price' => '5,000.00',
62  'price_type' => 'fixed',
63  'sku' => '3-2-select',
64  ],
65  ]
66  ],
67  [
68  'previous_group' => 'select',
69  'title' => 'Test Radio',
70  'type' => 'radio',
71  'is_require' => 1,
72  'sort_order' => 0,
73  'values' => [
74  [
75  'option_type_id' => null,
76  'title' => 'Option 1',
77  'price' => '600.234',
78  'price_type' => 'fixed',
79  'sku' => '4-1-radio',
80  ],
81  [
82  'option_type_id' => null,
83  'title' => 'Option 2',
84  'price' => '40,000.00',
85  'price_type' => 'fixed',
86  'sku' => '4-2-radio',
87  ],
88  ]
89  ],
90  [
91  'previous_group' => 'text',
92  'title' => 'Test Field',
93  'type' => 'field',
94  'is_require' => 1,
95  'sort_order' => 0,
96  'price' => 1,
97  'price_type' => 'fixed',
98  'sku' => '1-text',
99  'max_characters' => 100,
100  ],
101 ];
102 
103 $options = [];
104 
106 $customOptionFactory = $objectManager->create(\Magento\Catalog\Api\Data\ProductCustomOptionInterfaceFactory::class);
107 
108 foreach ($oldOptions as $option) {
110  $option = $customOptionFactory->create(['data' => $option]);
111  $option->setProductSku($product->getSku());
112 
113  $options[] = $option;
114 }
115 
116 $product->setOptions($options);
117 
119 $productRepositoryFactory = $objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class);
121 
122 $categoryLinkManagement->assignProductToCategories(
123  $product->getSku(),
124  [2]
125 );