You are here

public function CartTable::addToCart in Basic cart 8

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

Overrides CartInterface::addToCart

File

src/CartTable.php, line 80

Class

CartTable
Class CartTable.

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) {
      $param['uid'] = $this->user_id;
      $param['id'] = $id;
      $param['entitytype'] = $params['entitytype'];

      // 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;
        $param['quantity'] = $_SESSION['basic_cart']['cart_quantity'][$id];
        $this->cart_storage
          ->update($param);
      }
      else {
        $entity = \Drupal::entityTypeManager()
          ->getStorage($entitytype)
          ->load($id);
        $_SESSION['basic_cart']['cart'][$id] = $entity;
        $_SESSION['basic_cart']['cart_quantity'][$id] = $quantity;
        $param['quantity'] = $_SESSION['basic_cart']['cart_quantity'][$id];
        $this->cart_storage
          ->insert($param);
      }
    }
    Settings::cartUpdatedMessage();
  }
}