示例#1
0
 /**
  * save a order for one store
  * @author cangzhou.wu(wucangzhou@gmail.com)
  * @param $carItems , it should be  ['sku_id'=> cartModel]
  * @param $starId
  * @return bool
  */
 public function saveSingleOrder($carItems, $starId)
 {
     $ShoppingCart = new ShoppingCart();
     $transaction = \Yii::$app->db->beginTransaction();
     try {
         $this->user_id = Yii::$app->user->id;
         $this->total_price = $ShoppingCart->getSubTotal(0, $starId);
         $this->shipping_fee = $ShoppingCart->getShippingFee();
         $this->payment_fee = 0;
         $this->status = self::STATUS_WAIT_PAYMENT;
         $this->changePrice();
         if ($this->save()) {
             foreach ($carItems as $cartItem) {
                 $sku = $cartItem->sku;
                 $item = $sku->item;
                 $price_true = $sku->price;
                 $orderItem = new OrderItem();
                 $orderItem->order_id = $this->order_id;
                 $orderItem->item_id = $sku->sku_id;
                 $orderItem->price = $price_true;
                 $orderItem->qty = $cartItem->qty;
                 $orderItem->name = $item->title;
                 $orderItem->picture = $item->getMainImage();
                 $orderItems[] = $orderItem;
                 if (!$orderItem->save()) {
                     throw new Exception('Unable to save order item record.');
                 }
             }
             foreach ($carItems as $cartItem) {
                 $ShoppingCart->remove($cartItem->sku_id);
             }
         } else {
             throw new Exception('Unable to save order record.');
         }
         $this->items = $orderItems;
         $this->updateItemStock();
         $transaction->commit();
         return true;
     } catch (\yii\base\Exception $e) {
         $transaction->rollback();
         return false;
     }
 }