Example #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;
 }