Skip to content

Commit

Permalink
Added Iventory::updateInventoryLevel()
Browse files Browse the repository at this point in the history
  • Loading branch information
nfourtythree committed Dec 5, 2024
1 parent f5ebebf commit f9b4fae
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 17 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@

- Added `craft\commerce\base\InventoryItemTrait`.
- Added `craft\commerce\base\InventoryLocationTrait`.
- Added `craft\commerce\base\Purchasable::setStockLevel()`.
- Added `craft\commerce\base\Purchasable::setStockLevel()`.
- Added `craft\commerce\services\Inventory::updateInventoryLevel()`.
19 changes: 3 additions & 16 deletions src/base/Purchasable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
26 changes: 26 additions & 0 deletions src/services/Inventory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Check failure on line 341 in src/services/Inventory.php

View workflow job for this annotation

GitHub Actions / ci / Code Quality / PHPStan / PHPStan

Call to an undefined method craft\commerce\services\Inventory::getStore().
'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
Expand Down

0 comments on commit f9b4fae

Please sign in to comment.