Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
product_export_data.php
Go to the documentation of this file.
1 <?php
7 require dirname(dirname(__DIR__)) . '/Catalog/_files/category.php';
9 require dirname(dirname(__DIR__)) . '/Store/_files/second_store.php';
11 require dirname(dirname(__DIR__)) . '/Catalog/_files/products_with_multiselect_attribute.php';
13 require dirname(dirname(__DIR__)) . '/Catalog/_files/product_text_attribute.php';
14 
16 
18 $productModel = $objectManager->create(\Magento\Catalog\Model\Product::class);
19 
21  [
22  'option_id' => null,
23  'sort_order' => '0',
24  'title' => 'Option 1',
25  'type' => 'drop_down',
26  'is_require' => 1,
27  'values' => [
28  1 => [
29  'option_type_id' => null,
30  'title' => 'Option 1 & Value 1"',
31  'price' => '1.00',
32  'price_type' => 'fixed'
33  ],
34  2 => [
35  'option_type_id' => null,
36  'title' => 'Option 1 & Value 2"',
37  'price' => '2.00',
38  'price_type' => 'fixed'
39  ],
40  3 => [
41  'option_type_id' => null,
42  'title' => 'Option 1 & Value 3"',
43  'price' => '3.00',
44  'price_type' => 'fixed'
45  ]
46  ]
47  ],
48  [
49  'title' => 'test_option_code_2',
50  'type' => 'field',
51  'is_require' => true,
52  'sort_order' => 1,
53  'price' => 10.0,
54  'price_type' => 'fixed',
55  'sku' => 'sku1',
56  'max_characters' => 10,
57  ],
58 ];
59 
60 $productModel->setTypeId(
61  \Magento\Catalog\Model\Product\Type::TYPE_SIMPLE
62 )->setId(
63  1
64 )->setAttributeSetId(
65  4
66 )->setName(
67  'New Product'
68 )->setSku(
69  'simple'
70 )->setPrice(
71  10
72 )->addData(
73  ['text_attribute' => '!@#$%^&*()_+1234567890-=|\\:;"\'<,>.?/']
74 )->setTierPrice(
75  [0 => ['website_id' => 0, 'cust_group' => 0, 'price_qty' => 3, 'price' => 8]]
76 )->setVisibility(
77  \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH
78 )->setStatus(
79  \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED
80 )->setWebsiteIds(
81  [1]
82 )->setStockData(
83  ['qty' => 100, 'is_in_stock' => 1, 'manage_stock' => 1]
84 )->setCanSaveCustomOptions(
85  true
86 )->setCategoryIds(
87  [333]
88 )->setUpSellLinkData(
89  [$product->getId() => ['position' => 1]]
90 );
91 
92 $options = [];
93 
95 $customOptionFactory = $objectManager->create(\Magento\Catalog\Api\Data\ProductCustomOptionInterfaceFactory::class);
96 
97 foreach ($customOptions as $option) {
99  $option = $customOptionFactory->create(['data' => $option]);
100  $option->setProductSku($productModel->getSku());
101 
102  $options[] = $option;
103 }
104 
105 $productModel->setOptions($options)->save();
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60
$customOptionFactory