Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
simple_product.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
15 
17 $objectManager = Bootstrap::getObjectManager();
18 
20 $product = $objectManager->create(Product::class);
21 $product->setTypeId('simple')
22  ->setAttributeSetId(4)
23  ->setName('Simple Product')
24  ->setSku('simple002')
25  ->setPrice(10)
26  ->setQty(100)
27  ->setVisibility(Visibility::VISIBILITY_BOTH)
28  ->setStatus(Status::STATUS_ENABLED);
29 
31 $stockItem = $objectManager->create(StockItemInterface::class);
32 $stockItem->setQty(100)
33  ->setIsInStock(true);
34 $extensionAttributes = $product->getExtensionAttributes();
35 $extensionAttributes->setStockItem($stockItem);
36 
38 $productRepository = $objectManager->get(ProductRepositoryInterface::class);
$stockItem
$productRepository
$extensionAttributes
$product
$objectManager