From 73b35ede65c0c68d052b544eaab91e102b928712 Mon Sep 17 00:00:00 2001 From: Luke Holder Date: Wed, 11 Dec 2024 18:52:34 +0800 Subject: [PATCH] Fix PHP error when editing inventory level in modal --- src/models/InventoryItem.php | 21 +++++++++++++-------- src/models/InventoryLevel.php | 6 ++++-- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/models/InventoryItem.php b/src/models/InventoryItem.php index c27a9ed7a4..9bd9afdb1d 100644 --- a/src/models/InventoryItem.php +++ b/src/models/InventoryItem.php @@ -52,20 +52,25 @@ class InventoryItem extends Model */ public ?\DateTime $dateUpdated = null; - public function getCpEditUrl(): string - { - return UrlHelper::cpUrl('commerce/inventory/item/' . $this->id); - } - + /** + * @var Purchasable|null + */ + private ?Purchasable $_purchasable = null; + /** * @return ?Purchasable + * @var null|string|int $siteId */ - public function getPurchasable(): ?Purchasable + public function getPurchasable(null|string|int $siteId = null): ?Purchasable { + if ($this->_purchasable !== null) { + return $this->_purchasable; + } + /** @var ?Purchasable $purchasable */ - $purchasable = \Craft::$app->getElements()->getElementById($this->purchasableId); + $this->_purchasable = \Craft::$app->getElements()->getElementById(elementId: $this->purchasableId, siteId: $siteId); - return $purchasable; + return $this->_purchasable; } public function getSku(): string diff --git a/src/models/InventoryLevel.php b/src/models/InventoryLevel.php index 0285ca8397..ae6ff3f8b3 100644 --- a/src/models/InventoryLevel.php +++ b/src/models/InventoryLevel.php @@ -129,9 +129,11 @@ public function getInventoryLocation(): InventoryLocation /** * @return Purchasable + * @var null|string|int $siteId */ - public function getPurchasable(): Purchasable + public function getPurchasable(null|string|int $siteId = null): Purchasable { - return $this->getInventoryItem()->getPurchasable(); + + return $this->getInventoryItem()->getPurchasable($siteId); } }