Пример #1
0
 /**
  * Places a new order
  */
 public function api_place_order()
 {
     $cart = Cart::get();
     $address = OrderAddress::get();
     $order = new Order();
     $order->setAttributes($address->attributes);
     $order->user_id = \Yii::$app->user->id;
     $order->currency_code = CurrencyHelper::current()->code;
     $order->discount = CurrencyHelper::convert($cart->discount);
     $order->status = Order::STATUS_NEW;
     try {
         if ($cart->total <= 0) {
             throw new Exception("Order with zero or less total can not be placed, ID={$order->id}");
         }
         if (!$order->save()) {
             throw new Exception("Unable to save order, ID={$order->id}");
         }
         foreach ($cart->lines as $line) {
             $orderLine = new OrderData();
             $orderLine->quantity = $line->quantity;
             $orderLine->product_id = $line->product_id;
             $orderLine->price = CurrencyHelper::convert($line->price);
             $order->link('orderLines', $orderLine);
         }
         $this->reduceStocks($order);
         $cart->clear();
     } catch (Exception $e) {
         \Yii::warning($e->getMessage());
         return false;
     }
     $this->sendOrderEmail($order, $order->email);
     return $order;
 }
Пример #2
0
 /**
  * @return string
  */
 public function actionAddress()
 {
     $cart = Cart::get();
     if (!$cart->lines) {
         return $this->redirect(['/checkout/cart/index']);
     }
     $address = new OrderAddress();
     if (!\Yii::$app->user->isGuest) {
         /** @var User $identity */
         $identity = \Yii::$app->user->identity;
         $address->setAttributes($identity->attributes);
     }
     if ($address->load(\Yii::$app->request->post()) && $address->validate()) {
         $address->save();
         $this->redirect(['place']);
     }
     return $this->render('address', ['address' => $address, 'countries' => Shop::countries(), 'page' => Shop::page(Pages::CHECKOUT)]);
 }