Exemplo 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]);
 }
Exemplo 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]);
 }
Exemplo n.º 3
0
 public function run()
 {
     DB::table('order_items')->delete();
     OrderItem::create(array('order_id' => '1', 'product_id' => '1', 'price' => '1000', 'quantity' => '4'));
     OrderItem::create(array('order_id' => '1', 'product_id' => '2', 'price' => '2000', 'quantity' => '4'));
     OrderItem::create(array('order_id' => '2', 'product_id' => '3', 'price' => '3000', 'quantity' => '1'));
     OrderItem::create(array('order_id' => '2', 'product_id' => '1', 'price' => '1000', 'quantity' => '2'));
     OrderItem::create(array('order_id' => '3', 'product_id' => '1', 'price' => '1000', 'quantity' => '2'));
 }
Exemplo n.º 4
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);
 }
 public function run()
 {
     DB::table('order_items')->delete();
     for ($i = 0; $i < $this->num_records; $i++) {
         $array = ["order_id" => $this->getRandRec(), "product_id" => $this->getRandRec(), "quantity" => $this->getFaker()->numberBetween($min = 1, $max = 20), 'price' => $this->getFaker()->randomFloat($nbMaxDecimals = 2, $min = 0, $max = 100)];
         OrderItem::create($array);
     }
 }
Exemplo n.º 6
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getOrderItems()
 {
     return $this->hasMany(OrderItem::className(), ['product_id' => 'id']);
 }
Exemplo n.º 7
0
 protected function getOrderItems($id)
 {
     $order_items = OrderItem::find(['order_id' => $id])->all();
     $items = ArrayHelper::map($order_items, 'item_id', 'count');
     return $items;
 }
Exemplo n.º 8
0
 public function addItem($product_id, $quantity)
 {
     $product = Product::findOrFail($product_id);
     $params = ['order_id' => $this->id, 'product_id' => $product->id, 'quantity' => $quantity, 'price' => $product->price * $quantity];
     return OrderItem::create($params);
 }
Exemplo n.º 9
0
 public function actionGetMenu()
 {
     $request = Yii::$app->request;
     $id = $request->post('id');
     $order_id = $request->post('order_id');
     $menu = $this->findModel($id);
     $order = $order_items = "";
     if ($order_id) {
         $order = Order::find($order_id)->one();
         $order_items = OrderItem::find(['order_id' => $order_id])->all();
         $order_items = ArrayHelper::map($order_items, 'item_id', 'count');
     }
     $items = ArrayHelper::map($menu->menuItems, 'item_id', 'item_name', 'item_tarif');
     return json_encode(['desc' => $menu->menu_desc, 'limit' => $menu->menu_limit, 'items' => $items, 'order_items' => $order_items]);
 }