Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
categories_with_product_ids.php
Go to the documentation of this file.
1 <?php
9  \Magento\Catalog\Setup\CategorySetup::class
10 );
11 require __DIR__ . '/categories.php';
12 
14 $productRepository = $objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class);
16  \Magento\Catalog\Api\CategoryLinkRepositoryInterface::class,
17  [
18  'productRepository' => $productRepository
19  ]
20 );
21 
24  \Magento\Catalog\Api\CategoryLinkManagementInterface::class,
25  [
26  'productRepository' => $productRepository,
27  'categoryLinkRepository' => $categoryLinkRepository
28  ]
29 );
30 
33 $product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
34  ->setAttributeSetId($installer->getAttributeSetId('catalog_product', 'Default'))
35  ->setStoreId(1)
36  ->setId(1)
37  ->setWebsiteIds([1])
38  ->setName('Simple Product')
39  ->setSku('simple')
40  ->setPrice(10)
41  ->setWeight(18)
42  ->setStockData(['use_config_manage_stock' => 0])
43  ->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
44  ->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
45  ->save();
46 $categoryLinkManagement->assignProductToCategories($product->getSku(), [2, 3, 4]);
47 
48 $product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Catalog\Model\Product::class);
49 $product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
50  ->setAttributeSetId($installer->getAttributeSetId('catalog_product', 'Default'))
51  ->setStoreId(1)
52  ->setId(2)
53  ->setWebsiteIds([1])
54  ->setName('Simple Product Two')
55  ->setSku('12345') // SKU intentionally contains digits only
56  ->setPrice(45.67)
57  ->setWeight(56)
58  ->setStockData(['use_config_manage_stock' => 0])
59  ->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
60  ->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
61  ->save();
62 $categoryLinkManagement->assignProductToCategories($product->getSku(), [5]);
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60