class CartSession in Commerce Core 8.2
Default implementation of the cart session.
Hierarchy
- class \Drupal\commerce_cart\CartSession implements CartSessionInterface
Expanded class hierarchy of CartSession
1 file declares its use of CartSession
- CheckoutController.php in modules/
checkout/ src/ Controller/ CheckoutController.php
1 string reference to 'CartSession'
- commerce_cart.services.yml in modules/
cart/ commerce_cart.services.yml - modules/cart/commerce_cart.services.yml
1 service uses CartSession
File
- modules/
cart/ src/ CartSession.php, line 10
Namespace
Drupal\commerce_cartView source
class CartSession implements CartSessionInterface {
/**
* The session.
*
* @var \Symfony\Component\HttpFoundation\Session\SessionInterface
*/
protected $session;
/**
* Constructs a new CartSession object.
*
* @param \Symfony\Component\HttpFoundation\Session\SessionInterface $session
* The session.
*/
public function __construct(SessionInterface $session) {
$this->session = $session;
}
/**
* {@inheritdoc}
*/
public function getCartIds($type = self::ACTIVE) {
$key = $this
->getSessionKey($type);
return $this->session
->get($key, []);
}
/**
* {@inheritdoc}
*/
public function addCartId($cart_id, $type = self::ACTIVE) {
$key = $this
->getSessionKey($type);
$ids = $this->session
->get($key, []);
$ids[] = $cart_id;
$this->session
->set($key, array_unique($ids));
}
/**
* {@inheritdoc}
*/
public function hasCartId($cart_id, $type = self::ACTIVE) {
$key = $this
->getSessionKey($type);
$ids = $this->session
->get($key, []);
return in_array($cart_id, $ids);
}
/**
* {@inheritdoc}
*/
public function deleteCartId($cart_id, $type = self::ACTIVE) {
$key = $this
->getSessionKey($type);
$ids = $this->session
->get($key, []);
$ids = array_diff($ids, [
$cart_id,
]);
if (!empty($ids)) {
$this->session
->set($key, $ids);
}
else {
// Remove the empty list to allow the system to clean up empty sessions.
$this->session
->remove($key);
}
}
/**
* Gets the session key for the given cart session type.
*
* @param string $type
* The cart session type.
*
* @return string
* The session key.
*
* @throws \InvalidArgumentException
* Thrown when the given $type is unknown.
*/
protected function getSessionKey($type) {
$keys = [
self::ACTIVE => 'commerce_cart_orders',
self::COMPLETED => 'commerce_cart_completed_orders',
];
if (!isset($keys[$type])) {
throw new \InvalidArgumentException(sprintf('Unknown cart session type "%s".', $type));
}
return $keys[$type];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CartSession:: |
protected | property | The session. | |
CartSession:: |
public | function |
Adds the given cart order ID to the session. Overrides CartSessionInterface:: |
|
CartSession:: |
public | function |
Deletes the given cart order id from the session. Overrides CartSessionInterface:: |
|
CartSession:: |
public | function |
Gets all cart order ids from the session. Overrides CartSessionInterface:: |
|
CartSession:: |
protected | function | Gets the session key for the given cart session type. | |
CartSession:: |
public | function |
Checks whether the given cart order ID exists in the session. Overrides CartSessionInterface:: |
|
CartSession:: |
public | function | Constructs a new CartSession object. | |
CartSessionInterface:: |
constant | |||
CartSessionInterface:: |
constant |