Ejemplo n.º 1
0
 public function actionOrder()
 {
     $order = new Orders();
     /* @var $cart ShoppingCart */
     $cart = \Yii::$app->cart;
     /* @var $products Product[] */
     $products = $cart->getPositions();
     $total = $cart->getCost();
     if ($order->load(Yii::$app->request->post()) && $order->validate()) {
         $transaction = $order->getDb()->beginTransaction();
         $order->save(false);
         foreach ($products as $product) {
             $orderItem = new OrderItem();
             $orderItem->order_id = $order->order_id;
             $orderItem->item_id = $product->item_id;
             $orderItem->price = $product->getPrice();
             $orderItem->quantity = $product->getQuantity();
             if (!$orderItem->save(false)) {
                 $transaction->rollBack();
                 Yii::$app->session->addFlash('error', 'Cannot place your order. Please contact us.');
                 // TODO: may need to add Yii::t()
                 return $this->redirect('../');
                 // redirect to web root
             }
         }
         $transaction->commit();
         \Yii::$app->cart->removeAll();
         Yii::$app->session->addFlash('success', 'Thanks for your order. We\'ll contact you soon.');
         $order->sendEmail();
         return $this->redirect('../');
         // redirect to web root
     }
     return $this->render('order', ['order' => $order, 'products' => $products, 'total' => $total]);
 }
Ejemplo n.º 2
0
 public function actionOrder()
 {
     $order = new Order();
     $products = $this->_cart->getPositions();
     $total = $this->_cart->getCost();
     if ($order->load(\Yii::$app->request->post()) && $order->validate()) {
         $order->user_id = 1;
         $order->cart_id = "text";
         if ($order->save(false)) {
             foreach ($products as $product) {
                 $orderItem = new OrderItem();
                 $orderItem->order_id = $order->id;
                 $orderItem->product_id = $product->id;
                 $orderItem->price = $product->getPrice();
                 $orderItem->quantity = $product->getQuantity();
                 if (!$orderItem->save(false)) {
                     \Yii::$app->session->addFlash('error', 'Cannot place your order. Please contact us.');
                     return $this->redirect('site/index');
                 }
             }
         }
         $this->_cart->removeAll();
         \Yii::$app->session->addFlash('success', 'Thanks for your order. We\'ll contact you soon.');
         //$order->sendEmail();
         return $this->redirect('site/index');
     }
     return $this->render('order', ['order' => $order, 'products' => $products, 'total' => $total]);
 }
Ejemplo n.º 3
0
 public function add($id)
 {
     $order = Order::where('id', '=', $id)->where('user_id', '=', Auth::user()->id)->first();
     //$orderItem = $order->items()->findOrFail($id);
     //$productId = $order->items()->findOrFail($itemId);
     $orderItem = $order->items()->where('product_id', '=', (int) Input::get("product_id"))->first();
     if ($orderItem) {
         $orderItem->quantity += (int) Input::get("quantity");
         $orderItem->save();
     } else {
         $product = Product::findOrFail((int) Input::get("product_id"));
         $orderItem = new OrderItem();
         $orderItem->order_id = $id;
         $orderItem->product_id = $product->id;
         $orderItem->price = $product->price;
         $orderItem->quantity = (int) Input::get("quantity");
         $orderItem->save();
     }
     $this->recalculateDelivery($order);
     return $this->orderToJson($order);
 }
Ejemplo n.º 4
0
 /**
  * Updates an existing Order model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id
  * @return mixed
  */
 public function actionUpdate($id)
 {
     $model = $this->findModel($id);
     $order_items = OrderItem::find(['order_id' => $id])->joinWith('item')->all();
     if ($model->load(Yii::$app->request->post())) {
         $request = Yii::$app->request;
         OrderItem::deleteAll(['order_id' => $id]);
         if (isset($request->post()['Order']['order_items'])) {
             $items = $request->post()['Order']['order_items'];
             foreach ($items as $key => $val) {
                 $double = OrderItem::find()->where(['item_id' => $key, 'order_id' => $id])->count();
                 if (!$double) {
                     $order_item = new OrderItem();
                     $order_item->item_id = $key;
                     $order_item->count = $val;
                     $order_item->order_id = $id;
                     $order_item->save();
                 }
             }
         }
         if ($model->save()) {
             return $this->redirect(['view', 'id' => $model->order_id]);
         }
     } else {
         return $this->render('update', ['model' => $model, 'locations' => $this->getAllLocations(), 'all_menu' => $this->getAllMenu(), 'addons' => Item::find()->asArray()->where(['item_type' => Item::ADDON_TYPE])->all(), 'order_items' => $this->getOrderItems($id)]);
     }
 }