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]); }
private function createAjax() { $data = Yii::$app->request->post(); $model = new Order(['scenario' => Order::SCENARIO_CREATE]); foreach ($data['Order'] as $key => $value) { $model->{$key} = $value; } if ($model->validate()) { return $model->save() ? true : ErrorHelper::errorsToString($model->errors); } else { return ErrorHelper::errorsToString($model->errors); } }
public function actionOrder($id) { $this->layout = 'page'; $model = new Order(); $categories = Category::findAll(['status' => Category::STATUS_ENABLED]); $product = Product::find()->where(['id' => $id, 'status' => Product::STATUS_ENABLED])->limit(1)->one(); if ($model->load(Yii::$app->request->post()) && $model->validate()) { $order = new Order(); $order->key = strtoupper(Yii::$app->security->generateRandomString(6)); $order->name = $product->name; $order->thumbnail = $product->thumbnail; $order->price = $product->price; $order->count = $model->count; $order->email = $model->email; $order->message = $model->message; $order->date = time(); $order->insert(); Yii::$app->session->set('orderID', $order->id); return $this->redirect(['product/confirm']); } else { return $this->render('order', ['model' => $model, 'categories' => $categories, 'product' => $product]); } }
/** *将完成的订单信息存入数据库 */ public function actionAddorder() { // 生成b_time count price(需要查询) // // $timeoffset = 8; // echo date('y-m-d h:i:s',time()); // $order->o_id = //怎么自动生成一个id // $b_id 在AddOder 中 要验证seat的状态 $order = new Order(); $res = Yii::$app->request; $order->o_id = $this->actionGetoid(); //$order->s_id = '*****@*****.**'; $order->s_id = Yii::$app->user->id; $order->seat_id = $res->post('seat_id'); $order->b_time = date('y-m-d h:i:s', time()); $order->count = count($_SESSION["dishlist"]); $order->desc = $res->post('desc'); $order->price = $this->actionAccprice(); if ($order->validate()) { foreach ($_SESSION['dishlist'] as $dish) { $one = new Orderdish(); $one->o_id = $order->o_id; $one->d_id = $dish['dish_id']; $one->count = $dish['num']; $one->price = $dish['price']; if ($one->validate() && $one->save()) { } else { error('error'); } } $order->save(); return 'success'; } else { return 'fales'; } }