protected function CartProvider::loadCartData in Commerce Core 8.2
Loads the cart data for the given user.
Parameters
\Drupal\Core\Session\AccountInterface $account: The user. If empty, the current user is assumed.
Return value
array The cart data.
2 calls to CartProvider::loadCartData()
- CartProvider::getCartId in modules/
cart/ src/ CartProvider.php - Gets the cart order ID for the given store and user.
- CartProvider::getCartIds in modules/
cart/ src/ CartProvider.php - Gets all cart order ids for the given user.
File
- modules/
cart/ src/ CartProvider.php, line 203
Class
- CartProvider
- Default implementation of the cart provider.
Namespace
Drupal\commerce_cartCode
protected function loadCartData(AccountInterface $account = NULL) {
$account = $account ?: $this->currentUser;
$uid = $account
->id();
if (isset($this->cartData[$uid])) {
return $this->cartData[$uid];
}
if ($account
->isAuthenticated()) {
$query = $this->orderStorage
->getQuery()
->condition('state', 'draft')
->condition('cart', TRUE)
->condition('uid', $account
->id())
->sort('order_id', 'DESC')
->accessCheck(FALSE);
$cart_ids = $query
->execute();
}
else {
$cart_ids = $this->cartSession
->getCartIds();
}
$this->cartData[$uid] = [];
if (!$cart_ids) {
return [];
}
// Getting the cart data and validating the cart IDs received from the
// session requires loading the entities. This is a performance hit, but
// it's assumed that these entities would be loaded at one point anyway.
/** @var \Drupal\commerce_order\Entity\OrderInterface[] $carts */
$carts = $this->orderStorage
->loadMultiple($cart_ids);
$non_eligible_cart_ids = [];
foreach ($carts as $cart) {
if ($cart
->isLocked()) {
// Skip locked carts, the customer is probably off-site for payment.
continue;
}
if ($cart
->getCustomerId() != $uid || empty($cart->cart->value) || $cart
->getState()
->getId() != 'draft') {
// Skip carts that are no longer eligible.
$non_eligible_cart_ids[] = $cart
->id();
continue;
}
$this->cartData[$uid][$cart
->id()] = [
'type' => $cart
->bundle(),
'store_id' => $cart
->getStoreId(),
];
}
// Avoid loading non-eligible carts on the next page load.
if (!$account
->isAuthenticated()) {
foreach ($non_eligible_cart_ids as $cart_id) {
$this->cartSession
->deleteCartId($cart_id);
}
}
return $this->cartData[$uid];
}