diff --git a/CHANGELOG.md b/CHANGELOG.md index 5abfb3a..c2ffd48 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## Unreleased + +### Fixed +- Fixed an error that occurred when using `craft.reorder.unavailableLineItems()` + ## 2.0.0-beta.1 - 2022-03-17 ### Added diff --git a/src/Variable.php b/src/Variable.php index 229bcb5..97466c5 100644 --- a/src/Variable.php +++ b/src/Variable.php @@ -2,8 +2,9 @@ namespace spicyweb\reorder; +use Craft; use craft\commerce\elements\Order; - +use craft\commerce\Plugin as Commerce; use spicyweb\reorder\Plugin as ReOrder; /** @@ -19,11 +20,24 @@ class Variable * Checks an order's line items and returns the unavailable items along with why they're unavailable. * * @param Order $order The order to check. - * @param int $cartId A cart ID, to check for the quantity of items already in the user's cart. + * @param Order|int|null $cart A cart, or cart ID, to check for the quantity of items already in the user's cart. * return array The line items that are unavailable and why. */ - public function unavailableLineItems(Order $order, int $cartId = null): array + public function unavailableLineItems(Order $order, Order|int|null $cart = null): array { - return ReOrder::$plugin->methods->getUnavailableLineItems($order, $cartId); + if (is_int($cart)) { + $cart = Commerce::getInstance()->getOrders()->getOrderById($cart); + } + + // Make sure the order and cart belong to the current user, otherwise tell them nothing + $currentUser = Craft::$app->getUser(); + $orderUser = $order->getCustomer(); + $cartUser = $cart !== null ? $cart->getCustomer() : null; + + if ((int)$currentUser->id !== (int)$orderUser->id || $cart !== null && (int)$currentUser->id !== (int)$cartUser->id) { + return []; + } + + return ReOrder::$plugin->methods->getUnavailableLineItems($order, $cart); } }