Example #1
0
 /**
  * Get detail information of a product
  * @param  long $productId - the unique ID of the product
  * @return an object of Product which contains information of the product
  */
 public function getProductObjectUsingId($productId)
 {
     $rowSet = Product::findFirst(array('conditions' => 'product_id = ?1', 'bind' => array(1 => $productId)));
     return $rowSet;
 }
 public function invoiceAction()
 {
     $this->view->product = Product::findFirst($_POST['product_id']);
     $this->view->user = User::findFirst($this->session->get("auth")['id']);
 }
Example #3
0
 public function checkoutAction()
 {
     //check if there is a logged in user
     if (!$this->session->get("auth")) {
         $this->flash->notice('Please login first');
         $this->response->redirect('Session');
     }
     //get logged in user instance and a list of the ids of the products in his cart
     $user = User::findFirst($this->session->get("auth")['id']);
     $products_ids = explode(", ", $user->cart);
     //check if cart is empty
     if (strlen($user->cart) < 1) {
         $this->response->redirect("Index");
         return $this->flash->notice("Cart is empty!");
     }
     //check if someone bought an item he already had in his cart
     foreach ($products_ids as $id) {
         $temp = Product::findFirst($id);
         if ($temp->stock <= 0) {
             $this->flash->notice("Error, please re-add items to your cart!");
             $user->cart = '';
             $user->save();
             return $this->response->redirect("Index");
         }
     }
     $products = array();
     $purchase = new Purchase();
     $purchase->user_id = $user->id;
     $purchase->time = new Phalcon\Db\RawValue('now()');
     //decrement each product's stock, and add purchase record data
     foreach ($products_ids as $id) {
         $temp = Product::findFirst($id);
         //added more items to cart than in stock, so only purchase available ones
         if ($temp->stock <= 0) {
             $this->flash->notice("Error, some items in your cart were not successfully purchased, please check your history");
             $user->cart = '';
             $user->save();
             $purchase->save();
             return $this->response->redirect("Index");
         }
         $temp->stock--;
         $temp->save();
         if (strlen($purchase->products) == 0 or is_null($purchase->products)) {
             $purchase->products = $id;
         } else {
             $purchase->products = $purchase->products . ', ' . $id;
         }
         $purchase->price += $temp->price;
     }
     //check if there is an error with saving the record
     if (!$purchase->save()) {
         foreach ($purchase->getMessages() as $message) {
             $this->flash->error($message);
         }
     } else {
         //empty cart and save
         $user->cart = '';
         $user->save();
     }
     $this->response->redirect("Index");
     $this->flash->success("Checkout successful!");
 }
 /**
  * @title("Remove")
  * @description("Remove a product")
  * @response("Result object or Error object")
  * @responseExample({
  *     "result": "OK"
  * })
  */
 public function delete($product_id)
 {
     $product = Product::findFirst((int) $product_id);
     if (!$product) {
         throw new UserException(ErrorCodes::DATA_NOTFOUND, 'Could not find product.');
     }
     if (!$product->delete()) {
         throw new UserException(ErrorCodes::DATA_FAIL, 'Could not remove product.');
     }
     return $this->respondOK();
 }