Skip to content

Commit

Permalink
Fix craft.reorder.unavailableLineItems()
Browse files Browse the repository at this point in the history
  • Loading branch information
ttempleton committed Mar 24, 2022
1 parent 6eae7e0 commit ba0d4d2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
22 changes: 18 additions & 4 deletions src/Variable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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);
}
}

0 comments on commit ba0d4d2

Please sign in to comment.