Exemplo n.º 1
0
 /**
  * Get back an array of products with the user selected quantity
  * @throws \Exception
  * @return array
  */
 public function getProducts()
 {
     if (isset($this->userSelectedProducts)) {
         return $this->userSelectedProducts;
     }
     $cartItems = $this->session->get('cart');
     $cartProductIds = $this->getProductIds();
     $dbProducts = $this->product->getProductsByProductIds($cartProductIds);
     /** @var array $userSelectedProducts Contains the merge of products/cart items */
     $userSelectedProducts = [];
     $grandTotal = 0.0;
     foreach ($cartItems as $cartItem) {
         foreach ($dbProducts as $dbProduct) {
             if ($dbProduct->product_id == $cartItem['product_id']) {
                 $dbProduct->quantity = $cartItem['quantity'];
                 $dbProduct->total_price = $dbProduct->price * $cartItem['quantity'];
                 $grandTotal += $dbProduct->total_price;
                 $userSelectedProducts[] = $dbProduct;
             }
         }
     }
     $this->grandTotal = $grandTotal;
     $this->userSelectedProducts = $userSelectedProducts;
     if (empty($userSelectedProducts)) {
         throw new \Exception('Please add some things to your cart!');
     }
     return $userSelectedProducts;
 }
Exemplo n.º 2
0
 /**
  * Gets the products within the cart and the total price
  * @return array
  * @throws \Exception
  */
 public function getCartProducts()
 {
     if (isset($this->userSelectedProducts)) {
         return $this->userSelectedProducts;
     }
     $cartItems = $this->session->get('cart');
     $product_id_in_cart = $this->getProductIdsInCart();
     $products = $this->product->getCartProducts($product_id_in_cart);
     $userSelectedProducts = [];
     $grandTotal = 0.0;
     foreach ($products as $item) {
         foreach ($cartItems as $cartItem) {
             if ($cartItem['product_id'] == $item->product_id) {
                 $item->quantity = $cartItem['quantity'];
                 $userSelectedProducts[] = $item;
                 $item->total = $cartItem['quantity'] * $item->price;
                 $grandTotal += $item->total;
             }
         }
     }
     $this->grandTotal = $grandTotal;
     $this->userSelectedProducts = $userSelectedProducts;
     return $userSelectedProducts;
 }