예제 #1
0
 public function actionShiWuOrder($slug)
 {
     $shopOrder = new ShopOrder();
     $shopProducts = ShopProduct::findOne(['slug' => $slug]);
     //myVarDump($shopOrder->validate());
     if ($shopOrder->load(\Yii::$app->request->post()) && $shopOrder->validate()) {
         $transaction = $shopOrder->getDb()->beginTransaction();
         $shopOrder->user_id = \Yii::$app->user->getId();
         $shopOrder->setOrderSn();
         $shopOrder->save(false);
         $shopOrderItem = new ShopOrderItem();
         $shopOrderItem->order_id = $shopOrder->id;
         $shopOrderItem->title = $shopProducts['title'];
         $shopOrderItem->price = $shopProducts['price'];
         $shopOrderItem->product_id = $shopProducts['id'];
         $shopOrderItem->quantity = 1;
         if (!$shopOrderItem->save(false)) {
             $transaction->rollBack();
             \Yii::$app->session->addFlash('error', '暂时不能处理您的订单!');
             return $this->redirect(['catalog/list']);
         }
         $transaction->commit();
         \Yii::$app->cart->removeAll();
         \Yii::$app->session->addFlash('success', '谢谢您的订单,我们会尽快处理!');
         $shopOrder->sendEmail();
         return $this->redirect(['product/my-order']);
     }
     return $this->render('shi-wu-order', ['shopOrder' => $shopOrder, 'shopProducts' => $shopProducts]);
 }