Skip to content

Commit

Permalink
Fixes #3668
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeholder committed Sep 13, 2024
1 parent 40f75e1 commit 4fe4ebe
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
10 changes: 10 additions & 0 deletions src/collections/UpdateInventoryLevelCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,14 @@ public static function make($items = [])
$collection = parent::make($items);
return $collection;
}

/**
* @return array
*/
public function getPurchasables(): array
{
return $this->map(function(UpdateInventoryLevel $updateInventoryLevel) {
return $updateInventoryLevel->inventoryItem->getPurchasable();
})->all();
}
}
7 changes: 4 additions & 3 deletions src/services/Inventory.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,9 @@ public function executeUpdateInventoryLevels(UpdateInventoryLevelCollection $upd

$transaction->commit();

// TODO: Update stock value on purchasable stores
// Craft::$app->getElements()->invalidateCachesForElement($this);
foreach ($updateInventoryLevels->getPurchasables() as $purchasable) {
Plugin::getInstance()->getPurchasables()->updateStoreStockCache($purchasable, true);
}

return true;
} catch (\Exception $e) {
Expand Down Expand Up @@ -734,7 +735,7 @@ public function orderCompleteHandler(Order $order)

foreach ($selectedInventoryLevelForItem as $inventoryLevel) {
$purchasable = $inventoryLevel->getPurchasable();
Plugin::getInstance()->getPurchasables()->updateStoreStockCache($purchasable);
Plugin::getInstance()->getPurchasables()->updateStoreStockCache($purchasable, true);
}
}
}
28 changes: 19 additions & 9 deletions src/services/Purchasables.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,20 +155,30 @@ public function isPurchasableShippable(PurchasableInterface $purchasable, Order
* Updated the cached stock value for the purchasable in a store.
*
* @param Purchasable $purchasable
* @param bool $allSites Update across all sites (stores).
* @return void
* @throws \yii\base\InvalidConfigException
* @throws \yii\db\Exception
*/
public function updateStoreStockCache(Purchasable $purchasable): void
public function updateStoreStockCache(Purchasable $purchasable, bool $allSites = false): void
{
$stock = Plugin::getInstance()->getInventory()->getInventoryLevelsForPurchasable($purchasable)->sum('availableTotal');

Craft::$app->getDb()->createCommand()
->update(
table:Table::PURCHASABLES_STORES,
columns: ['stock' => $stock],
condition: ['id' => $purchasable->id, 'storeId' => $purchasable->getStore()->id])
->execute();
if ($allSites) {
$purchasables = $purchasable::find()->siteid('*')->id($purchasable->id)->status(null)->all();
} else {
$purchasables = [$purchasable];
}

/** @var Purchasable $purchasable */
foreach ($purchasables as $purchasable) {
$stock = Plugin::getInstance()->getInventory()->getInventoryLevelsForPurchasable($purchasable)->sum('availableTotal');

Craft::$app->getDb()->createCommand()
->update(
table: Table::PURCHASABLES_STORES,
columns: ['stock' => $stock],
condition: ['purchasableId' => $purchasable->id, 'storeId' => $purchasable->getStore()->id])
->execute();
}
}

/**
Expand Down

0 comments on commit 4fe4ebe

Please sign in to comment.