/** * 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; }
/** * @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)]); }
<?php use app\helpers\CurrencyHelper; use app\modules\checkout\models\Cart; use yii\bootstrap\Nav; use yii\helpers\Url; $cart = Cart::get(); Nav::begin(['encodeLabels' => false, 'items' => [['label' => "<strong>" . CurrencyHelper::format($cart->total) . "</strong> ( {$cart->totalCount} )", 'url' => null], ['label' => '<i class="glyphicon glyphicon-shopping-cart"></i>', 'active' => true, 'url' => Url::to(['/checkout/cart/index'])]], 'options' => ['class' => 'mini-cart navbar-nav pull-right']]); Nav::end();
/** * @return string */ public function actionIndex() { $cart = Cart::get(); $cart->normalize(); return $this->render('index', ['page' => Shop::page(Pages::SHOPPING_CART), 'cart' => $cart]); }