Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
product_simple_with_url_key.php
Go to the documentation of this file.
1 <?php
9 $product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
10  ->setAttributeSetId(4)
11  ->setWebsiteIds([1])
12  ->setName('Simple Product')
13  ->setSku('simple1')
14  ->setPrice(10)
15  ->setDescription('Description with <b>html tag</b>')
16  ->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
17  ->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
18  ->setCategoryIds([2])
19  ->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1])
20  ->setUrlKey('url-key')
21  ->save();
22 
24 $product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Catalog\Model\Product::class);
25 $product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
26  ->setAttributeSetId(4)
27  ->setWebsiteIds([1])
28  ->setName('Simple Product 2')
29  ->setSku('simple2')
30  ->setPrice(10)
31  ->setDescription('Description with <b>html tag</b>')
32  ->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
33  ->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
34  ->setCategoryIds([2])
35  ->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1])
36  ->setUrlKey('url-key2')
37  ->save();