/** * remove all items */ public function actionClearAll() { $shoppingCartModel = new ShoppingCart(); if ($shoppingCartModel->clearAll()) { echo Json::encode(['message' => 'remove success', 'redirect' => 'index']); } else { echo Json::encode(['message' => 'remove fail', 'redirect' => 'index']); } }
?> "> <?php } else { ?> <tr> <td colspan="6" style="padding:10px">您的购物车是空的!</td> </tr> <?php } } ?> <tr> <td colspan="6" style="padding:10px;text-align:right"> 运费:<?php $shoppingCart = new ShoppingCart(); echo $shoppingCart->getShippingFee(); ?> 元 </td> </tr> <tr> <td colspan="6" style="padding:10px;text-align:right"> 总计:<?php echo $shoppingCart->getTotal(); ?> 元 </td> </tr> </table>
/** * 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; } }
/** * checkout view * @author cangzhou.wu(wucangzhou@gmail.com) * @return string * @throws \yii\web\NotFoundHttpException */ public function actionCheckout() { $star_id = (int) Yii::$app->request->get('star_id'); $shoppingCart = new ShoppingCart(); $cartItems = $shoppingCart->cartItems; if ($star_id) { $cartItems = $shoppingCart->serialCartItems(); if (isset($cartItems[$star_id])) { $cartItems = $cartItems[$star_id]; } else { throw new NotFoundHttpException('The requested page does not exist.'); } } return $this->render('index', ['cartItems' => $cartItems]); }