Example #1
0
 /**
  * @param $item
  * @return array
  */
 public static function add($item, $isCart = false)
 {
     if (Cart::verifyStock($item['product_id'], $item['quantity'])) {
         $item['row_id'] = hash('md5', $item['product_id'] . '_' . $item['var_id']);
         $cart_items = self::getItems();
         if (!empty($cart_items) and self::checkIsExist($item)) {
             if (!$isCart) {
                 $cart_items[$item['row_id']]['quantity'] = self::updateStock($item['row_id'], $item['quantity']);
             } else {
                 $cart_items[$item['row_id']] = $item;
             }
         } else {
             $cart_items[$item['row_id']] = $item;
         }
         if (empty($item)) {
             return array();
         }
         self::setItems($cart_items);
         return true;
     } else {
         return false;
     }
 }
Example #2
0
 /**
  * @return $this
  */
 public function getCartFromSession()
 {
     //initialise
     $update_cart_session = false;
     //get items from the cart
     /**
      * If we process the product with "Cart::getItems", then item meta are move to array format.
      * Raw array format is not suitable for using "$this->meta->meta_key", so product is not yet
      * processing.
      */
     $cart_items = Cart::getItems(true);
     foreach ($cart_items as $key => $cartitem) {
         //let us find the product
         $product = $cartitem->getProduct();
         if ($product && !is_null($product)) {
             //does the product exists
             if ($product->getId() && $product->exists() && $cartitem->getQuantity() > 0) {
                 if (!$product->isPurchasable()) {
                     //product is unavailable. Set a flag indicating that the cart session has to be updated.
                     $update_cart_session = true;
                     do_action('sp_remove_cart_item_from_session', $key, $cartitem);
                 } else {
                     $cartitem->put('product', $product);
                     $this->cart_contents[$key] = apply_filters('cartrabbit_get_cart_item_from_session', $cartitem, $key);
                 }
             }
         }
     }
     // Trigger action
     do_action('sp_cart_loaded_from_session', $this);
     if (!$this->subtotal && !$this->isEmpty()) {
         $this->calculateTotals();
     }
     return $this;
 }