0

I'm trying to save a new product with variants via PHP.

$product = \Craft::$app->elements->createElement(\craft\commerce\elements\Product::class);
$product->typeId = 1;
$product->enabled = true;
$product->enabledForSite = true;

$variant = \Craft::$app->elements->createElement(\craft\commerce\elements\Variant::class);
$variant->sku = '2304978';
(plus other custom fields)

$product->setVariants([$variant]);
\Craft::$app->elements->saveElement($product);

This code results in an exception.

Invalid product ID: 412122
#0 /path/to/craft/vendor/craftcms/commerce/src/elements/Variant.php(895): craft\commerce\elements\Variant->getProduct()
#1 /path/to/craft/vendor/yiisoft/yii2/base/Model.php(350): craft\commerce\elements\Variant->beforeValidate()
#2 /path/to/craft/vendor/craftcms/cms/src/services/Elements.php(536): yii\base\Model->validate()
#3 /path/to/craft/vendor/craftcms/cms/src/services/Elements.php(2037): craft\services\Elements->saveElement(Object(craft\commerce\elements\Variant), true, false)
#4 /path/to/craft/vendor/craftcms/cms/src/services/Elements.php(654): craft\services\Elements->_propagateElement(Object(craft\commerce\elements\Variant), false, Array)
#5 /path/to/craft/vendor/craftcms/commerce/src/elements/Product.php(715): craft\services\Elements->saveElement(Object(craft\commerce\elements\Variant), false)
#6 /path/to/craft/vendor/craftcms/cms/src/services/Elements.php(645): craft\commerce\elements\Product->afterSave(true)
#7 /path/to/craft/modules/customModule/src/console/controllers/ImportController.php(186): craft\services\Elements->saveElement(Object(craft\commerce\elements\Product))
#8 /path/to/craft/modules/customModule/src/console/controllers/ImportController.php(205): modules\customModule\console\controllers\ImportController->modules\customModule\console\controllers\{closure}(Object(stdClass), Object(craft\models\Site))
#9 [internal function]: modules\customModule\console\controllers\ImportController->actionDoSomething()
#10 /path/to/craft/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#11 /path/to/craft/vendor/yiisoft/yii2/base/Controller.php(157): yii\base\InlineAction->runWithParams(Array)
#12 /path/to/craft/vendor/yiisoft/yii2/console/Controller.php(148): yii\base\Controller->runAction('do-something', Array)
#13 /path/to/craft/vendor/yiisoft/yii2/base/Module.php(528): yii\console\Controller->runAction('do-something', Array)
#14 /path/to/craft/vendor/yiisoft/yii2/console/Application.php(180): yii\base\Module->runAction('customModule/im...', Array)
#15 /path/to/craft/vendor/craftcms/cms/src/console/Application.php(93): yii\console\Application->runAction('customModule/im...', Array)
#16 /path/to/craft/vendor/yiisoft/yii2/console/Application.php(147): craft\console\Application->runAction('customModule/im...', Array)
#17 /path/to/craft/vendor/yiisoft/yii2/base/Application.php(386): yii\console\Application->handleRequest(Object(craft\console\Request))
#18 /path/to/craft/craft(26): yii\base\Application->run()
#19 {main}"
  • Not a proper answer, but I'd model it off of what ProductsController->actionSaveProduct() and helpers\Product::populateProductFromPost() are doing. – Brad Bell Jan 9 at 0:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.