Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
product_simple_with_all_fields.php
Go to the documentation of this file.
1 <?php
8 include __DIR__ . '/product_simple_with_full_option_set.php';
9 
12 
14 $productRepository = $objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class);
15 
16 $category = $objectManager->create(\Magento\Catalog\Model\Category::class);
17 $category->isObjectNew(true);
18 $category->setId(10)
19  ->setName('Movable Position 2')
20  ->setParentId(2)
21  ->setPath('1/2/10')
22  ->setLevel(2)
23  ->setAvailableSortBy(['name', 'price'])
24  ->setDefaultSortBy('name')
25  ->setIsActive(true)
26  ->setPosition(6)
27  ->save();
28 
29 $category = $objectManager->create(\Magento\Catalog\Model\Category::class);
30 $category->isObjectNew(true);
31 $category->setId(1151)
32  ->setName('Filter category')
33  ->setParentId(10)
34  ->setPath('1/2/10/1151')
35  ->setLevel(3)
36  ->setAvailableSortBy(['name', 'price'])
37  ->setDefaultSortBy('name')
38  ->setIsActive(true)
39  ->setPosition(0)
40  ->save();
41 
42 $product = $productRepository->get('simple', true);
44  'category_ids' => [10],
45  'cost' => 2.234,
46  'country_of_manufacture' => 'US',
47  'msrp' => 10.48,
48  'gift_message_available' => 0,
49  'msrp_display_actual_price_type' => 0,
50  'news_from_date' => '2017-08-10',
51  'news_to_date' => '2017-08-11',
52  'old_id' => 35235,
53  'options_container' => 'Options Container',
54  'required_options' => 1,
55  'special_price' => 3.82,
56  'special_from_date' => date('Y-m-d', strtotime('-1 day')),
57  'special_to_date' => date('Y-m-d', strtotime('+1 day')),
58  'manufacturer' => 'Magento Inc.',
59 ];
60 
61 foreach ($eavAttributeValues as $attributeCode => $attributeValue) {
62  $product->setCustomAttribute($attributeCode, $attributeValue);
63 }
64 
66 
68 $categoryLinkManagement = $objectManager->create(\Magento\Catalog\Api\CategoryLinkManagementInterface::class);
69 
70 $categoryLinkManagement->assignProductToCategories(
71  $product->getSku(),
72  [2, 10, 1151]
73 );
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60
$attributeCode
Definition: extend.phtml:12