Skip to content

Commit

Permalink
Merge pull request #3882 from craftcms/feature/pt-2418-purchasable-st…
Browse files Browse the repository at this point in the history
…ock-cache-should-be-updated-on-movements-also

Stock cache should be updated on movements
  • Loading branch information
lukeholder authored Feb 6, 2025
2 parents 6438d29 + 5c7397f commit f2d739f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

## 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 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()`.
- Added `craft\commerce\base\Gateway::transactionSupportsRefund()`.
- 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 the `commerce/cart/update-cart` action could return unnecessary validation errors. ([3873](https://github.com/craftcms/commerce/issues/3873))

## 5.3.1 - 2025-02-03
Expand Down
11 changes: 11 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,14 @@
*/
class InventoryMovementCollection extends Collection
{
/**
* @return array
* @since 5.3.2
*/
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
13 changes: 10 additions & 3 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 @@ -381,7 +382,7 @@ public function updatePurchasableInventoryLevel(Purchasable $purchasable, int $q
$this->updateInventoryLevel($purchasable->inventoryItemId, $quantity, $updateInventoryLevelAttributes);

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

/**
Expand Down Expand Up @@ -530,8 +531,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 f2d739f

Please sign in to comment.