Skip to content

Commit

Permalink
Fixes for inventory purchasable getters
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeholder committed Dec 11, 2024
1 parent 0ef668b commit 60f705f
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/collections/UpdateInventoryLevelCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static function make($items = [])
public function getPurchasables(): array
{
return $this->map(function(UpdateInventoryLevel|UpdateInventoryLevelInTransfer $updateInventoryLevel) {
return $updateInventoryLevel->inventoryItem->getPurchasable();
return $updateInventoryLevel->inventoryItem->getPurchasable('*');
})->all();
}
}
5 changes: 3 additions & 2 deletions src/controllers/TransfersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use craft\commerce\models\TransferDetail;
use craft\commerce\Plugin;
use craft\commerce\services\Transfers;
use craft\helpers\Cp as CraftCp;
use craft\helpers\Html;
use craft\helpers\StringHelper;
use craft\helpers\UrlHelper;
Expand Down Expand Up @@ -267,8 +268,8 @@ public function actionReceiveTransferScreen(): Response
$tableRows = '';
foreach ($transfer->getDetails() as $detail) {
$key = $detail->uid;
$purchasable = $detail->getInventoryItem()?->getPurchasable();
$label = $purchasable ? \craft\helpers\Cp::elementChipHtml($purchasable) : $detail->inventoryItemDescription;
$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']);
Expand Down
4 changes: 2 additions & 2 deletions src/fieldlayoutelements/TransferManagementField.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public static function renderStaticFieldHtml(Transfer $element, bool $static = f
$tableRows = '';

foreach ($element->getDetails() as $detail) {
$purchasable = $detail->getInventoryItem()?->getPurchasable();
$purchasable = $detail->getInventoryItem()?->getPurchasable(Cp::requestedSite()->id);
$tableRows .= Html::tag('tr',
Html::tag('td', ($purchasable ? Cp::chipHtml($purchasable, ['showActionMenu' => !$purchasable->getIsDraft() && $purchasable->canSave($currentUser)]) : Html::tag('span', $detail->inventoryItemDescription))) .
Html::tag('td', (string)$detail->quantityRejected, ['class' => 'rightalign']) .
Expand Down Expand Up @@ -188,7 +188,7 @@ public static function renderFieldHtml(Transfer $element): string

foreach ($element->getDetails() as $detail) {
$key = $detail->uid ?? StringHelper::UUID();
$purchasable = $detail->getInventoryItem()?->getPurchasable();
$purchasable = $detail->getInventoryItem()?->getPurchasable(Cp::requestedSite()->id);
$tableRows .= Html::tag('tr',
Html::hiddenInput('details[' . $key . '][id]', (string)$detail->id) .
Html::hiddenInput('details[' . $key . '][uid]', $detail->uid) .
Expand Down
4 changes: 2 additions & 2 deletions src/models/InventoryFulfillmentLevel.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ public function getLineItem(): LineItem
/**
* @return Purchasable
*/
public function getPurchasable(): Purchasable
public function getPurchasable(null|string|int $siteId = null): Purchasable
{
return $this->getInventoryItem()->getPurchasable();
return $this->getInventoryItem()->getPurchasable($siteId);
}
}
9 changes: 5 additions & 4 deletions src/models/InventoryItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace craft\commerce\models;

use Craft;
use craft\commerce\base\Model;
use craft\commerce\base\Purchasable;

Expand Down Expand Up @@ -58,23 +59,23 @@ class InventoryItem extends Model

/**
* @return ?Purchasable
* @var null|string|int $siteId
*/
public function getPurchasable(null|string|int $siteId = null): ?Purchasable
{
if ($this->_purchasable !== null) {
return $this->_purchasable;
}

/** @var ?Purchasable $purchasable */
$this->_purchasable = \Craft::$app->getElements()->getElementById(elementId: $this->purchasableId, siteId: $siteId);
/** @phpstan-ignore-next-line */
$this->_purchasable = Craft::$app->getElements()->getElementById(elementId: $this->purchasableId, siteId: $siteId);

/** @phpstan-ignore-next-line */
return $this->_purchasable;
}

public function getSku(): string
{
return $this->getPurchasable()->sku;
return $this->getPurchasable('*')->sku;
}

protected function defineRules(): array
Expand Down
1 change: 0 additions & 1 deletion src/models/InventoryLevel.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ public function getInventoryLocation(): InventoryLocation

/**
* @return Purchasable
* @var null|string|int $siteId
*/
public function getPurchasable(null|string|int $siteId = null): Purchasable
{
Expand Down
6 changes: 3 additions & 3 deletions src/services/Inventory.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,9 @@ public function executeUpdateInventoryLevels(UpdateInventoryLevelCollection $upd

$transaction->commit();

foreach ($updateInventoryLevels->getPurchasables() as $purchasable) {
Plugin::getInstance()->getPurchasables()->updateStoreStockCache($purchasable, true);
}
// Update all purchasables stock
$purchasables = $updateInventoryLevels->getPurchasables();
Plugin::getInstance()->getPurchasables()->updateStoreStockCache($purchasables[0], true);

return true;
} catch (\Exception $e) {
Expand Down
5 changes: 4 additions & 1 deletion src/services/Purchasables.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,10 @@ public function isPurchasableShippable(PurchasableInterface $purchasable, Order
public function updateStoreStockCache(Purchasable $purchasable, bool $allSites = false): void
{
if ($allSites) {
$purchasables = $purchasable::find()->siteid('*')->id($purchasable->id)->status(null)->all();
$purchasables = $purchasable::find()
->siteId('*')
->id($purchasable->id)
->status(null)->all();
} else {
$purchasables = [$purchasable];
}
Expand Down

0 comments on commit 60f705f

Please sign in to comment.