Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
simple_product_20.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
16 
18 $objectManager = Bootstrap::getObjectManager();
19 
21 $product = $objectManager->create(Product::class);
22 $product->isObjectNew(true);
23 $product->setTypeId(Type::TYPE_SIMPLE)
24  ->setAttributeSetId(4)
25  ->setWebsiteIds([1])
26  ->setName('Simple Product 20')
27  ->setSku('simple_20')
28  ->setPrice(20)
29  ->setWeight(1)
30  ->setVisibility(Visibility::VISIBILITY_BOTH)
31  ->setStatus(Status::STATUS_ENABLED);
32 
34 $stockItem = $objectManager->create(StockItemInterface::class);
35 $stockItem->setQty(100)
36  ->setIsInStock(true);
37 $extensionAttributes = $product->getExtensionAttributes();
38 $extensionAttributes->setStockItem($stockItem);
39 
41 $productRepository = $objectManager->get(ProductRepositoryInterface::class);
$objectManager
$extensionAttributes
$productRepository