You are here

public function CartSession::addToCart in Basic cart 8

Same name and namespace in other branches
  1. 8.6 src/CartSession.php \Drupal\basic_cart\CartSession::addToCart()
  2. 8.0 src/CartSession.php \Drupal\basic_cart\CartSession::addToCart()
  3. 8.2 src/CartSession.php \Drupal\basic_cart\CartSession::addToCart()
  4. 8.3 src/CartSession.php \Drupal\basic_cart\CartSession::addToCart()
  5. 8.4 src/CartSession.php \Drupal\basic_cart\CartSession::addToCart()
  6. 8.5 src/CartSession.php \Drupal\basic_cart\CartSession::addToCart()

Overrides CartInterface::addToCart

File

src/CartSession.php, line 70

Class

CartSession
Class CartSession.

Namespace

Drupal\basic_cart

Code

public function addToCart($id, $params = array()) {
  $config = Settings::cartSettings();
  if (!empty($params)) {
    $quantity = $params['quantity'];
    $entitytype = $params['entitytype'];
    $quantity = $params['quantity'];
    if ($id > 0 && $quantity > 0) {

      // If a node is added more times, just update the quantity.
      $cart = self::getCart();
      if ($config
        ->get('quantity_status') && !empty($cart['cart']) && in_array($id, array_keys($cart['cart']))) {

        // Clicked 2 times on add to cart button. Increment quantity.
        $_SESSION['basic_cart']['cart_quantity'][$id] += $quantity;
      }
      else {
        $entity = \Drupal::entityTypeManager()
          ->getStorage($entitytype)
          ->load($id);
        $_SESSION['basic_cart']['cart'][$id] = $entity;
        $_SESSION['basic_cart']['cart_quantity'][$id] = $quantity;
      }
    }
    Settings::cartUpdatedMessage();
  }
}