The docs for v1 mention a method craft()->commerce_purchasables->saveElement()
.
This is omitted in the docs for v2. Does anyone know how I can create a product programmatically in PHP?
$product = new Product();
$product->title = "Test Product 1";
$product->typeId = 2;
$product->enabled = true;
$product->promotable = true;
$variant = new Variant();
$variant->isDefault = true;
$product->setVariants([$variant]);
Craft::$app->getElements()->saveElement($product);
With the above code I get the following error:
Call to a member function getProductTypes() on null
I know the typeId is correct, because I can output it on a product template with {{ product.typeId }}