Skip to content

[5.x]: How to create a Product in Craft Commerce 5 (Controller) #3458

Closed Answered by alex-brukhty
alex-brukhty asked this question in Q&A
Discussion options

You must be logged in to vote

made it work by re-querying variants again after creating new and saving as above.

foreach (Variant::find()->id($variantIds)->all() as $variant) {
  $updateInventoryLevels = UpdateInventoryLevelCollection::make();
  $updateInventoryLevels->push(
    new UpdateInventoryLevel([
      'type' => 'available',
      'updateAction' => InventoryUpdateQuantityType::SET,
      'inventoryItem' => $variant->getInventoryItem(),
      'inventoryLocation' => Plugin::getInstance()->getInventoryLocations()->getInventoryLocationById(1),
      'quantity' => 1,
      'note' => 'New',
    ])
  );
  Plugin::getInstance()->getInventory()->executeUpdateInventoryLevels($updateInventoryLevels);
}

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@bhashkar007
Comment options

Answer selected by alex-brukhty
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants