在magento2中以编程方式创建简单产品

Vin*_*mar 6 magento magento2

Magento的-2:

如何从magento-2外部以编程方式创建一个简单的产品

小智 2

我不知道你所说的外部是什么意思,但我能够使用magerun2创建一个产品。这就是我走了多远:

# bin/n98-magerun2.phar dev:console

$productFactory = $objectManager->create("\Magento\Catalog\Model\ProductFactory");

// this needs to be set to avoid exception:
// Magento\Framework\Exception\SessionException with message 'Area code not set: Area code must be set before starting a session.'
// I don't know why ...
$appState = $objectManager->get("Magento\Framework\App\State")
$appState->setAreaCode("de")

$product = $productFactory->create()

$product->setName("FooBar")
$product->setName("123")
$product->setAttributeSetId(15)

$product->save()
Run Code Online (Sandbox Code Playgroud)