diff --git a/CHANGELOG-WIP.md b/CHANGELOG-WIP.md index cd6c262b92..90f6838d32 100644 --- a/CHANGELOG-WIP.md +++ b/CHANGELOG-WIP.md @@ -11,4 +11,5 @@ - Added `craft\commerce\base\InventoryItemTrait`. - Added `craft\commerce\base\InventoryLocationTrait`. -- Added `craft\commerce\base\Purchasable::setStockLevel()`. \ No newline at end of file +- Added `craft\commerce\base\Purchasable::setStockLevel()`. +- Added `craft\commerce\services\Inventory::updateInventoryLevel()`. \ No newline at end of file diff --git a/src/base/Purchasable.php b/src/base/Purchasable.php index 3c88c51113..2c094b4c77 100644 --- a/src/base/Purchasable.php +++ b/src/base/Purchasable.php @@ -956,28 +956,15 @@ public function getStock(): int /** * @param int $quantity - * @param array $updateAttributes + * @param array $updateInventoryLevelAttributes * @return void * @throws InvalidConfigException * @throws Exception * @since 5.3.0 */ - public function setStockLevel(int $quantity, array $updateAttributes = []): void + public function setStockLevel(int $quantity, array $updateInventoryLevelAttributes = []): void { - $updateAttributes += [ - 'quantity' => $quantity, - 'updateAction' => InventoryUpdateQuantityType::SET, - 'inventoryLocationId' => $this->getStore()->getInventoryLocations()->first()->id, - 'type' => InventoryTransactionType::AVAILABLE->value, - ]; - - $updateInventoryLevel = new UpdateInventoryLevel($updateAttributes); - $updateInventoryLevel->inventoryItemId = $this->inventoryItemId; - - $updateInventoryLevels = UpdateInventoryLevelCollection::make(); - $updateInventoryLevels->push($updateInventoryLevel); - - Plugin::getInstance()->getInventory()->executeUpdateInventoryLevels($updateInventoryLevels); + Plugin::getInstance()->getInventory()->updateInventoryLevel($this->inventoryItemId, $quantity, $updateInventoryLevelAttributes); $this->_stock = null; } diff --git a/src/services/Inventory.php b/src/services/Inventory.php index a3324876a0..0dc088b619 100644 --- a/src/services/Inventory.php +++ b/src/services/Inventory.php @@ -325,6 +325,32 @@ public function executeUpdateInventoryLevels(UpdateInventoryLevelCollection $upd } } + /** + * @param int $inventoryItemId + * @param int $quantity + * @param array $updateInventoryLevelAttributes + * @return void + * @throws Exception + * @throws InvalidConfigException + */ + public function updateInventoryLevel(int $inventoryItemId, int $quantity, array $updateInventoryLevelAttributes = []) + { + $updateInventoryLevelAttributes += [ + 'quantity' => $quantity, + 'updateAction' => InventoryUpdateQuantityType::SET, + 'inventoryLocationId' => $this->getStore()->getInventoryLocations()->first()->id, + 'type' => InventoryTransactionType::AVAILABLE->value, + ]; + + $updateInventoryLevel = new UpdateInventoryLevel($updateInventoryLevelAttributes); + $updateInventoryLevel->inventoryItemId = $inventoryItemId; + + $updateInventoryLevels = UpdateInventoryLevelCollection::make(); + $updateInventoryLevels->push($updateInventoryLevel); + + Plugin::getInstance()->getInventory()->executeUpdateInventoryLevels($updateInventoryLevels); + } + /** * @param UpdateInventoryLevel|UpdateInventoryLevelInTransfer $updateInventoryLevel * @return bool