提交 cede4043 编写于 作者: T Terry

购物车,增加产品的active个数和所有购物车产品总数数据

上级 bcf136ac
......@@ -26,6 +26,9 @@ use Yii;
*/
class Quote extends Service
{
/**
* 购物车的个数计算,是否仅仅计算active状态的产品个数总和,如果设置false,则将购物车中所有的产品个数累加。
*/
const SESSION_CART_ID = 'current_session_cart_id';
protected $_cart_id;
......@@ -203,17 +206,20 @@ class Quote extends Service
/**
* @param $item_qty | Int
* 当$item_qty为null时,从cart items表中查询产品总数。
* 当$item_qty 不等于null时,代表已经知道购物车中产品的个数,不需要去cart_item表中查询,譬如清空购物车操作,直接就知道产品个数肯定为零。
* 当$active_item_qty为null时,从cart items表中查询产品总数。
* 当$item_qty 不等于null时,代表已经知道购物车中active产品的个数,不需要去cart_item表中查询,譬如清空购物车操作,直接就知道产品个数肯定为零。
* 当购物车的产品变动后,会调用该函数,更新cart表的产品总数
*/
public function computeCartInfo($item_qty = null)
public function computeCartInfo($active_item_qty = null)
{
if ($item_qty === null) {
$item_qty = Yii::$service->cart->quoteItem->getItemQty();
$items_all_count = 0;
if ($active_item_qty === null) {
$active_item_qty = Yii::$service->cart->quoteItem->getActiveItemQty();
}
$items_all_count = Yii::$service->cart->quoteItem->getItemAllQty();
$cart = $this->getCart();
$cart->items_count = $item_qty;
$cart->items_all_count = $items_all_count;
$cart->items_count = $active_item_qty;
$cart->save();
return true;
......@@ -360,9 +366,11 @@ class Quote extends Service
return false;
}
$cart = $this->getCart();
$items_qty = $cart['items_count'];
if ($items_qty <= 0) {
// 购物车中所有的产品个数
$items_all_count = $cart['items_all_count'];
// 购物车中active状态的产品个数
$items_count = $cart['items_count'];
if ($items_count <=0 && $items_all_count <= 0) {
return false;
}
$coupon_code = $cart['coupon_code'];
......
......@@ -175,7 +175,7 @@ class QuoteItem extends Service
* 在购物车中产品有变动后,使用这个函数得到产品总数,更新购物车中
* 的产品总数。
*/
public function getItemQty()
public function getItemAllQty()
{
$cart_id = Yii::$service->cart->quote->getCartId();
$item_qty = 0;
......@@ -192,6 +192,31 @@ class QuoteItem extends Service
return $item_qty;
}
/**
* 通过quoteItem表,计算得到所有产品的总数
* 得到购物车中产品的总数,不要使用这个函数,这个函数的作用:
* 在购物车中产品有变动后,使用这个函数得到产品总数,更新购物车中
* 的产品总数。
*/
public function getActiveItemQty()
{
$cart_id = Yii::$service->cart->quote->getCartId();
$item_qty = 0;
if ($cart_id) {
$data = $this->_itemModel->find()->asArray()->where([
'cart_id' => $cart_id,
'active' => $this->activeStatus,
])->all();
if (is_array($data) && !empty($data)) {
foreach ($data as $one) {
$item_qty += $one['qty'];
}
}
}
return $item_qty;
}
/**
* @param $activeProduct | boolean , 是否只要active的产品
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册