Skip to content

Commit

Permalink
实现未登陆可添加购物车功能
Browse files Browse the repository at this point in the history
  • Loading branch information
EleTeam committed Jan 2, 2017
1 parent 4a637a2 commit 84f6b08
Show file tree
Hide file tree
Showing 10 changed files with 319 additions and 38 deletions.
3 changes: 2 additions & 1 deletion common/models/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ public function attributeLabels()
*/
public function getCartItems()
{
return $this->hasMany(CartItem::className(), ['cart_id' => 'id']);
return $this->hasMany(CartItem::className(), ['cart_id' => 'id'])
->where('is_ordered=:is_ordered and status!=:status', [':is_ordered'=>self::NO, ':status'=>self::STATUS_DELETED]);
}

/**
Expand Down
11 changes: 10 additions & 1 deletion common/models/CartItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
*
* @property Cart $cart
* @property Product $product
* @property CartItemAttrs[] $cartItemAttrs
* @property ProductSku $productSku
* @property CartItemAttr[] $cartItemAttrs
*/
class CartItem extends ETActiveRecord
{
Expand Down Expand Up @@ -106,6 +107,14 @@ public function getProduct()
return $this->hasOne(Product::className(), ['id' => 'product_id']);
}

/**
* @return \yii\db\ActiveQuery
*/
public function getProductSku()
{
return $this->hasOne(ProductSku::className(), ['id' => 'sku_id']);
}

/**
* @return \yii\db\ActiveQuery
*/
Expand Down
18 changes: 18 additions & 0 deletions common/models/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,24 @@ public function getProductSkus()
->where('status != :status', [':status'=>self::STATUS_DELETED]);
}

/**
* 获得当前产品具体的ProductSku
*
* @param $sku_id
* @return ProductSku|null
*/
public function getProductSkuBy($sku_id)
{
if($this->productSkus){
foreach($this->productSkus as $productSku){
if($productSku->id == $sku_id){
return $productSku;
}
}
}
return null;
}

/**
* @return \yii\db\ActiveQuery
*/
Expand Down
38 changes: 13 additions & 25 deletions wap/controllers/CartController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,33 +28,21 @@ class CartController extends BaseController
*/
public function actionIndex()
{
// $cartItemsArr = [];
// $cart_num = 0;
// $total_price = 0;
// $app_cart_cookie_id = $this->getAppCartCookieId();
//
// $cart = Cart::myCart($this->getUserId(), $app_cart_cookie_id);
// if($cart) {
// $items = Cart::findItems($cart->id);
// foreach($items as $cartItem){
// $cartItemsArr[] = $cartItem->toArray([], ['product']);
// }
// $total_price = Cart::sumTotalPriceByItems($items);
// $cart_num = Cart::sumCartNumByItems($items);
// }
$cart_num = 0;
$total_price = 0;
$app_cart_cookie_id = $this->getAppCartCookieId();

// $data = [
// 'cart' => $cart,
// 'couponItems' => null,
// 'cart_num' => $cart_num,
// 'total_price' => $total_price,
// 'is_logged_in' => $this->isLoggedIn(),
// 'app_cart_cookie_id' => $app_cart_cookie_id,
// ];
return $this->render('index', [
// 'cart' => $cart,
// 'total_price' => $total_price,
$cart = Cart::myCart($this->getUserId(), $app_cart_cookie_id);
if($cart) {
$total_price = Cart::sumTotalPriceByItems($cart->cartItems);
$cart_num = Cart::sumCartNumByItems($cart->cartItems);
}

return $this->render('index', [
'cart' => $cart,
'total_price' => $total_price,
'cart_num' => $cart_num,
'is_logged_in' => $this->isLoggedIn(),
]);
}

Expand Down
81 changes: 76 additions & 5 deletions wap/views/cart/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,88 @@
*/

use yii\helpers\Html;
use common\models\Cart;

/* @var $this yii\web\View */
/* @var $banners */
/**
* @var $this yii\web\View
* @var $cart Cart
* @var $total_price float
* @var $cart_num int
* @var $is_logged_in int
*/
?>
<div class="navbar">
<div class="navbar-inner">
<div class="center sliding">购物车</div>
</div>
</div>
<div data-page="cart" class="page">
<div class="page-content">
购物车
<div class="page" data-page="cart">
<div class="page-content cart">
<?php if($cart && $cart->cartItems): ?>
<div class="shoppingCarMod">
<div class="shopping-group">
<div class="list-block shoppingCar-list-block media-list">
<ul>
<?php foreach($cart->cartItems as $cartItem): ?>
<li class="item-content swipeout" data-cart-item-id="<?=$cartItem->id?>" data-product-id="<?=$cartItem->product->id?>">
<div class="swipeout-content item-content">
<label class="label-checkbox">
<input name="my-checkbox" data-id="6711471333122342" type="checkbox">
<div class="item-media"><i class="icon icon-form-checkbox"></i></div>
</label>
<div class="pro-pic" data-id="6711471333122342">
<a class="pro-info">
<img src="<?=$cartItem->product->image_small?>" onerror="this.src='../image/no_image.jpg';" width="100">
</a>
</div>
<div class="item-inner">
<div class="title" data-id="6711471333122342"><?=$cartItem->product->id?></div>
<div class="item-title-row">
<p class="type" data-id="6711471333122342">色号:MB自然肤色 </p>
</div>
<div class="item-title-row oppRule_choose">
<div class="price" data-id="6711471333122342">¥ <?=$cartItem->productSku->price?></div>
<div class="choose text-center" data-stock="199">
<a href="#" class="plus" name="plus" data-cartid="70011">-</a>
<input readonly="readonly" class="num_value" value="<?=$cartItem->count?>" type="text">
<a href="#" class="add" name="add" data-cartid="70011">+</a>
</div>
<input name="leastNum" value="1" type="hidden">
</div>

</div>
</div>
<div class="swipeout-actions-right">
<a href="#" class="action1">
<!-- <i class="fa fa-trash-o fa-2x"></i> -->
删除
</a>
</div>
</li>
<div class="seperator padding-none"></div>
<?php endforeach; ?>
</ul>
</div>
</div>
</div>
<?php else: ?>
<div class="cart-empty" style="display: block;">
<div class="empty-tips">
<div>
<a href="html/uyac-login.html">
登录后可同步账户购物车中的商品
</a>
</div>
<div class="float-right">
<a href="html/uyac-login.html"><i class="icon icon-right"></i></a>
</div>
</div>
<div class="noPro">
<img src="../image/ic_cart_empty.png">
<div>购物车没有商品哦~</div>
<div id="goShopping"><a href="#" class="">去逛逛</a></div>
</div>
</div>
<?php endif ?>
</div>
</div>
Loading

0 comments on commit 84f6b08

Please sign in to comment.