Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
products_virtual.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
8 use Magento\Catalog\Api\Data\ProductInterfaceFactory;
10 use Magento\Catalog\Api\Data\ProductWebsiteLinkInterfaceFactory;
23 
24 $objectManager = Bootstrap::getObjectManager();
26 $productFactory = $objectManager->get(ProductInterfaceFactory::class);
28 $productRepository = $objectManager->get(ProductRepositoryInterface::class);
29 $productRepository->cleanCache();
31 $productWebsiteLinkRepository = $objectManager->get(ProductWebsiteLinkRepositoryInterface::class);
33 $productWebsiteLinkFactory = $objectManager->get(ProductWebsiteLinkInterfaceFactory::class);
35 $websiteRepository = $objectManager->get(WebsiteRepositoryInterface::class);
37 
39  'VIRT-1' => [
40  'qty' => 33,
41  'is_in_stock' => true,
42  'manage_stock' => true
43  ],
44  'VIRT-2' => [
45  'qty' => 30,
46  'is_in_stock' => true,
47  'manage_stock' => true
48  ],
49  'VIRT-3' => [
50  'qty' => 2,
51  'is_in_stock' => true,
52  'manage_stock' => true
53  ],
54  'VIRT-4' => [
55  'qty' => 6,
56  'is_in_stock' => false,
57  'manage_stock' => true
58  ]
59 ];
60 
61 foreach ($productData as $sku => $stockData) {
62  $product = $productFactory->create();
63  $product->setTypeId(Type::TYPE_VIRTUAL)
64  ->setAttributeSetId(4)
65  ->setName('Virtual Product ' . $sku)
66  ->setSku($sku)
67  ->setPrice(10)
68  ->setStockData($stockData)
69  ->setStatus(Status::STATUS_ENABLED);
71 
72  foreach ($websites as $website) {
73  if ($website->getCode() === 'admin') {
74  continue;
75  }
76 
78  $websiteLink = $productWebsiteLinkFactory->create();
79  $websiteLink->setSku($sku);
80  $websiteLink->setWebsiteId($website->getId());
81  $productWebsiteLinkRepository->save($websiteLink);
82  }
83 }
84 
86 $moduleManager = Bootstrap::getObjectManager()->get(Manager::class);
87 // soft dependency in tests because we don't have possibility replace fixture from different modules
88 if ($moduleManager->isEnabled('Magento_InventoryCatalog')) {
90  $searchCriteriaBuilder = Bootstrap::getObjectManager()->get(SearchCriteriaBuilder::class);
92  $defaultSourceProvider = $objectManager->get(DefaultSourceProviderInterface::class);
94  $sourceItemRepository = $objectManager->get(SourceItemRepositoryInterface::class);
96  $sourceItemsDelete = $objectManager->get(SourceItemsDeleteInterface::class);
97 
98  // Unassign created product from default Source
100  ->addFilter(SourceItemInterface::SKU, ['VIRT-1', 'VIRT-2', 'VIRT-3', 'VIRT-4'], 'in')
101  ->addFilter(SourceItemInterface::SOURCE_CODE, $defaultSourceProvider->getCode())
102  ->create();
103  $sourceItems = $sourceItemRepository->getList($searchCriteria)->getItems();
104  if (count($sourceItems)) {
106  }
107 }
$moduleManager
$objectManager
$productWebsiteLinkFactory
$productRepository
$searchCriteria
$sourceItems
$websiteRepository
$productFactory
$productData
$searchCriteriaBuilder
$sourceItemRepository
$productWebsiteLinkRepository
$stockData
Definition: products.php:27