Skip to content

Commit

Permalink
Stock cache should be updated on movements too
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeholder committed Feb 5, 2025
1 parent 9c396df commit 41a5fa4
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

## Unreleased

- Fixed a bug where soft-deleted variants were not being restored when the product was restored. ([#3815](https://github.com/craftcms/commerce/issues/3815))
- Fixed a bug where soft-deleted variants were not being restored when the product was restored. ([#3815](https://github.com/craftcms/commerce/issues/3815))
- Fixed a bug where inventory movements did not update the purchasable’s per-store cached stock total.
- Fixed a bug where transfers that contained deleted inventory items could not be updated.
- Added `craft\commerce\collections\InventoryMovementCollection::getPurchasables()`.

## 5.3.1 - 2025-02-03

Expand Down
10 changes: 10 additions & 0 deletions src/collections/InventoryMovementCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace craft\commerce\collections;

use craft\commerce\base\InventoryMovement;
use craft\commerce\base\InventoryMovementInterface;
use Illuminate\Support\Collection;

/**
Expand All @@ -22,4 +23,13 @@
*/
class InventoryMovementCollection extends Collection
{
/**
* @return array
*/
public function getPurchasables(): array
{
return $this->map(function(InventoryMovementInterface $updateInventoryLevel) {
return $updateInventoryLevel->getInventoryItem()->getPurchasable();
})->all();
}
}
17 changes: 13 additions & 4 deletions src/controllers/TransfersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public function actionReceiveTransfer(): Response
$transferDetails = $transfer->getDetails();

foreach ($transferDetails as $detail) {
if ($acceptedAmount = $details[$detail->uid]['accept']) {
if ($acceptedAmount = $details[$detail->uid]['accept'] ?? null) {
// Update the total accepted
$detail->quantityAccepted += $acceptedAmount;

Expand All @@ -193,7 +193,7 @@ public function actionReceiveTransfer(): Response
$inventoryMovementCollection->push($inventoryAcceptedMovement);
}

if ($rejectedAmount = $details[$detail->uid]['reject']) {
if ($rejectedAmount = $details[$detail->uid]['reject'] ?? null) {
// Update the total rejected
$detail->quantityRejected += $rejectedAmount;

Expand Down Expand Up @@ -255,18 +255,27 @@ public function actionReceiveTransferScreen(): Response

$tableRows = '';
foreach ($transfer->getDetails() as $detail) {
$deleted = $detail->inventoryItemId == null;
$key = $detail->uid;
$purchasable = $detail->getInventoryItem()?->getPurchasable(CraftCp::requestedSite()->id);
$label = $purchasable ? CraftCp::elementChipHtml($purchasable) : $detail->inventoryItemDescription;
$tableRows .= Html::beginTag('tr');
$tableRows .= Html::tag('td', $label);
$tableRows .= Html::tag('td', (string)$detail->quantityAccepted, ['class' => 'rightalign']);
$tableRows .= Html::tag('td',
Html::input('number', 'details[' . $key . '][accept]', '', ['class' => 'text fullwidth'])
Html::input('number', 'details[' . $key . '][accept]', '', [
'class' => 'text fullwidth',
'disabled' => $deleted,
'placeholder' => $deleted ? Craft::t('app', '“{name}” deleted.', ['name' => $detail->inventoryItemDescription]) : ''
])
);
$tableRows .= Html::tag('td', (string)$detail->quantityRejected, ['class' => 'rightalign']);
$tableRows .= Html::tag('td',
Html::input('number', 'details[' . $key . '][reject]', '', ['class' => 'text fullwidth'])
Html::input('number', 'details[' . $key . '][reject]', '', [
'class' => 'text fullwidth',
'disabled' => $deleted,
'placeholder' => $deleted ? Craft::t('app', '“{name}” deleted.', ['name' => $detail->inventoryItemDescription]) : ''
])
);
}

Expand Down
14 changes: 9 additions & 5 deletions src/services/Inventory.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ public function executeUpdateInventoryLevels(UpdateInventoryLevelCollection $upd

$transaction->commit();

// TODO: Potentially move this to a job in the queue
// Update all purchasables stock
$purchasables = $updateInventoryLevels->getPurchasables();
if ($purchasables) {
Expand Down Expand Up @@ -379,9 +380,6 @@ public function updatePurchasableInventoryLevel(Purchasable $purchasable, int $q
];

$this->updateInventoryLevel($purchasable->inventoryItemId, $quantity, $updateInventoryLevelAttributes);

// Clear the stock cache for the class instance
unset($purchasable->stock);
}

/**
Expand Down Expand Up @@ -530,8 +528,14 @@ public function executeInventoryMovements(InventoryMovementCollection $inventory

$transaction->commit();

// TODO: Update stock value on purchasable stores
// Craft::$app->getElements()->invalidateCachesForElement($this);
// TODO: Potentially move this to a job in the queue
foreach ($inventoryMovements as $inventoryMovement) {
// Update all purchasables stock
$purchasable = $inventoryMovement->getInventoryItem()->getPurchasable();
if ($purchasable) {
Plugin::getInstance()->getPurchasables()->updateStoreStockCache($purchasable, true);
}
}

return true;
} catch (\Exception $e) {
Expand Down

0 comments on commit 41a5fa4

Please sign in to comment.