Esempio n. 1
0
 /**
  * Finds the ShopProduct model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return ShopProduct the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = ShopProduct::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
Esempio n. 2
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]);
 }