You are here

class CartStorageSelect in Basic cart 8.6

Same name and namespace in other branches
  1. 8 src/CartStorageSelect.php \Drupal\basic_cart\CartStorageSelect
  2. 8.0 src/CartStorageSelect.php \Drupal\basic_cart\CartStorageSelect
  3. 8.2 src/CartStorageSelect.php \Drupal\basic_cart\CartStorageSelect
  4. 8.3 src/CartStorageSelect.php \Drupal\basic_cart\CartStorageSelect
  5. 8.4 src/CartStorageSelect.php \Drupal\basic_cart\CartStorageSelect
  6. 8.5 src/CartStorageSelect.php \Drupal\basic_cart\CartStorageSelect

Class CartStorageSelect to select either db or session.

Hierarchy

Expanded class hierarchy of CartStorageSelect

File

src/CartStorageSelect.php, line 8

Namespace

Drupal\basic_cart
View source
class CartStorageSelect {
  private $cart = NULL;
  private $cartStorage;

  /**
   * Construct object using  whether it is table class or session class.
   *
   * @param object $user
   *   Drupal user object.
   * @param bool $use_table
   *   Boolean to define the storage type.
   */
  public function __construct($user, $use_table = NULL) {
    $enable = $user
      ->id() && $use_table ? $user
      ->id() : 0;
    switch ($enable) {
      case 0:
        $this->cart = new CartSession($user);
        break;
      default:
        $cartStorage = new CartStorage();
        $this->cart = new CartTable($cartStorage, $user);
        break;
    }
  }

  /**
   * Get the cart data.
   *
   * @param int $nid
   *   Node id of the basic cart content.
   *
   * @return array
   *   array of cart data
   */
  public function getCart($nid = NULL) {
    return $this->cart
      ->getCart($nid);
  }

  /**
   * Remove the cart data by nid.
   *
   * @param int $nid
   *   Removes the node from cart.
   *
   * @return bool
   *   return true or false
   */
  public function removeFromCart($nid) {
    return $this->cart
      ->removeFromCart($nid);
  }

  /**
   * Empty the cart.
   *
   * @return string
   *   Text message
   */
  public function emptyCart() {
    return $this->cart
      ->emptyCart();
  }

  /**
   * Add the content to cart.
   *
   * @param int $id
   *   Node id of node.
   * @param array $params
   *   Will contain quantity and entity type if exists.
   *
   * @return string
   *   Message
   */
  public function addToCart($id, array $params = array()) {
    return $this->cart
      ->addToCart($id, $params);
  }

  /**
   * Sync cart data in session and database.
   */
  public function loggedInActionCart() {
    return $this->cart
      ->loggedInActionCart();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CartStorageSelect::$cart private property
CartStorageSelect::$cartStorage private property
CartStorageSelect::addToCart public function Add the content to cart.
CartStorageSelect::emptyCart public function Empty the cart.
CartStorageSelect::getCart public function Get the cart data.
CartStorageSelect::loggedInActionCart public function Sync cart data in session and database.
CartStorageSelect::removeFromCart public function Remove the cart data by nid.
CartStorageSelect::__construct public function Construct object using whether it is table class or session class.