Beispiel #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;
 }