public function getOrderItem() { return $this->hasMany(OrderItem::className(), ['order_id' => 'order_id']); }
/** * 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 = Yii::createObject(ShoppingCart::className()); $transaction = \Yii::$app->db->beginTransaction(); try { $this->user_id = Yii::$app->user->id; $this->shipping_fee = $ShoppingCart->getShippingFee(); $this->payment_fee = 0; $this->total_price = $ShoppingCart->getSubTotal(0, $starId) + $this->shipping_fee + $this->payment_fee; $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 = Yii::createObject(OrderItem::className()); $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; } }