QuoteItem.php 22.5 KB
Newer Older
_
__FresHmaN 已提交
1
<?php
S
Success Gao 已提交
2 3

/*
_
__FresHmaN 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16
 * FecShop file.
 *
 * @link http://www.fecshop.com/
 * @copyright Copyright (c) 2016 FecShop Software LLC
 * @license http://www.fecshop.com/license/
 */

namespace fecshop\services\cart;

use fecshop\services\Service;
use Yii;

/**
T
Terry 已提交
17
 * Cart services. 对购物车产品操作的具体实现部分。
_
__FresHmaN 已提交
18 19 20 21 22
 * @author Terry Zhao <2358269014@qq.com>
 * @since 1.0
 */
class QuoteItem extends Service
{
23
    public $itemDefaultActiveStatus = 1;
24

25
    public $activeStatus = 1;
26

27 28
    public $noActiveStatus = 2;
    
_
__FresHmaN 已提交
29
    protected $_my_cart_item;    // 购物车cart item 对象
30

_
__FresHmaN 已提交
31
    protected $_cart_product_info;
32 33
    
    protected $_itemModelName = '\fecshop\models\mysqldb\cart\Item';
34

S
Success Gao 已提交
35 36 37
    /**
     * @var \fecshop\models\mysqldb\cart\Item
     */
38 39
    protected $_itemModel;
    
S
Success Gao 已提交
40 41
    public function init()
    {
T
Terry 已提交
42
        parent::init();
S
Success Gao 已提交
43
        list($this->_itemModelName, $this->_itemModel) = Yii::mapGet($this->_itemModelName);
44 45
    }
    
_
__FresHmaN 已提交
46
    /**
S
Success Gao 已提交
47 48 49 50 51
     * 将某个产品加入到购物车中。
     * 在添加到 cart_item 表后,更新购物车中产品的总数。
     * @param array $item
     * @return mixed
     * example:
_
__FresHmaN 已提交
52 53 54 55
     * $item = [
     *		'product_id' 		=> 22222,
     *		'custom_option_sku' => red-xxl,
     *		'qty' 				=> 22,
56
     *      'sku' 				=> 'xxxx',
_
__FresHmaN 已提交
57 58 59 60 61 62 63 64 65 66 67
     * ];
     */
    public function addItem($item)
    {
        $cart_id = Yii::$service->cart->quote->getCartId();
        if (!$cart_id) {
            Yii::$service->cart->quote->createCart();
            $cart_id = Yii::$service->cart->quote->getCartId();
        }
        // 查看是否存在此产品,如果存在,则相加个数
        if (!isset($item['product_id']) || empty($item['product_id'])) {
S
Success Gao 已提交
68
            Yii::$service->helper->errors->add('add to cart error, product id is empty');
_
__FresHmaN 已提交
69 70 71 72 73

            return false;
        }
        $where = [
            'cart_id'    => $cart_id,
T
Terry 已提交
74
            'product_id' => $item['product_id'],
_
__FresHmaN 已提交
75 76 77 78
        ];
        if (isset($item['custom_option_sku']) && !empty($item['custom_option_sku'])) {
            $where['custom_option_sku'] = $item['custom_option_sku'];
        }
S
Success Gao 已提交
79
        /** @var \fecshop\models\mysqldb\cart\Item $item_one */
T
Terry 已提交
80
        $item_one = $this->_itemModel->find()->where($where)->one();
_
__FresHmaN 已提交
81
        if ($item_one['cart_id']) {
S
Success Gao 已提交
82
            $item_one->active = $this->itemDefaultActiveStatus;
_
__FresHmaN 已提交
83 84 85 86 87
            $item_one->qty = $item['qty'] + $item_one['qty'];
            $item_one->save();
            // 重新计算购物车的数量
            Yii::$service->cart->quote->computeCartInfo();
        } else {
88
            $item_one = new $this->_itemModelName;
S
Success Gao 已提交
89 90 91 92 93 94 95
            $item_one->store = Yii::$service->store->currentStore;
            $item_one->cart_id = $cart_id;
            $item_one->created_at = time();
            $item_one->updated_at = time();
            $item_one->product_id = $item['product_id'];
            $item_one->qty = $item['qty'];
            $item_one->active = $this->itemDefaultActiveStatus;
_
__FresHmaN 已提交
96 97
            $item_one->custom_option_sku = ($item['custom_option_sku'] ? $item['custom_option_sku'] : '');
            $item_one->save();
S
Success Gao 已提交
98
            // 重新计算购物车的数量,并写入 sales_flat_cart 表存储
_
__FresHmaN 已提交
99 100
            Yii::$service->cart->quote->computeCartInfo();
        }
T
Terry 已提交
101 102 103
        
        $item['afterAddQty'] = $item_one->qty;
        $this->sendTraceAddToCartInfoByApi($item);
S
Success Gao 已提交
104 105

        return true;
106
    }
107

108
    /**
T
Terry 已提交
109
     * @param $item | Array, example:
110 111 112
     * $item = [
     *		'product_id' 		=> 22222,
     *		'custom_option_sku' => red-xxl,
T
Terry 已提交
113
     *		'qty' 				=> 22,    // 添加购物车的产品个数
114
     *      'sku' 				=> 'xxxx',
T
Terry 已提交
115
     *      'afterAddQty'       => 33,  // 添加后,该产品在sku中的个数,这个个数是为了计算购物车中产品的价格
116
     * ];
S
Success Gao 已提交
117
     * 将加入购物车的操作,加入trace
118
     */
S
Success Gao 已提交
119 120
    public function sendTraceAddToCartInfoByApi($item)
    {
121
        if (Yii::$service->page->trace->traceJsEnable) {
T
Terry 已提交
122
            $product_price_arr  = Yii::$service->product->price->getCartPriceByProductId($item['product_id'], $item['afterAddQty'], $item['custom_option_sku'], 2);
123
            $base_product_price = isset($product_price_arr['base_price']) ? $product_price_arr['base_price'] : 0;
T
Terry 已提交
124
            // $price = $base_product_price * $item['qty'];
125 126 127
            $trace_cart_info = [
                [
                    'sku'   => $item['sku'],
T
Terry 已提交
128
                    'price' => $base_product_price,
129 130 131 132 133
                    'qty'   => $item['qty'],
                ]
            ];
            Yii::$service->page->trace->sendTraceAddToCartInfoByApi($trace_cart_info);
        }
_
__FresHmaN 已提交
134 135 136
    }

    /**
T
Terry 已提交
137
     * @param $item | Array, example:
_
__FresHmaN 已提交
138 139 140 141 142 143 144
     * $item = [
     *		'product_id' 		=> 22222,
     *		'custom_option_sku' => red-xxl,
     *		'qty' 				=> 22,
     * ];
     * @return boolean;
     *                  将购物车中的某个产品更改个数,更改后的个数就是上面qty的值。
S
fix  
Success Gao 已提交
145
     * @deprecated 该函数已经被遗弃
_
__FresHmaN 已提交
146
     */
S
fix  
Success Gao 已提交
147
    /*
_
__FresHmaN 已提交
148 149 150 151
    public function changeItemQty($item)
    {
        $cart_id = Yii::$service->cart->quote->getCartId();
        // 查看是否存在此产品,如果存在,则更改
T
Terry 已提交
152
        $item_one = $this->_itemModel->find()->where([
T
Terry 已提交
153 154 155
            'cart_id'           => $cart_id,
            'product_id'        => $item['product_id'],
            'custom_option_sku' => $item['custom_option_sku'],
_
__FresHmaN 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168 169
        ])->one();
        if ($item_one['cart_id']) {
            $item_one->qty = $item['qty'];
            $item_one->save();
            // 重新计算购物车的数量
            Yii::$service->cart->quote->computeCartInfo();

            return true;
        } else {
            Yii::$service->helper->errors->add('This product is not available in the shopping cart');

            return false;
        }
    }
T
Terry 已提交
170
    */
_
__FresHmaN 已提交
171 172 173 174 175 176 177

    /**
     * 通过quoteItem表,计算得到所有产品的总数
     * 得到购物车中产品的总数,不要使用这个函数,这个函数的作用:
     * 在购物车中产品有变动后,使用这个函数得到产品总数,更新购物车中
     * 的产品总数。
     */
178
    public function getItemAllQty()
_
__FresHmaN 已提交
179 180 181 182
    {
        $cart_id = Yii::$service->cart->quote->getCartId();
        $item_qty = 0;
        if ($cart_id) {
T
Terry 已提交
183
            $data = $this->_itemModel->find()->asArray()->where([
_
__FresHmaN 已提交
184 185 186 187 188 189 190 191 192 193 194
                'cart_id' => $cart_id,
            ])->all();
            if (is_array($data) && !empty($data)) {
                foreach ($data as $one) {
                    $item_qty += $one['qty'];
                }
            }
        }

        return $item_qty;
    }
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
    
    /**
     * 通过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;
    }
_
__FresHmaN 已提交
220 221

    /**
T
Terry 已提交
222
     * @param $activeProduct | boolean , 是否只要active的产品
_
__FresHmaN 已提交
223 224 225 226 227 228 229 230 231
     * @return array , foramt:
     *               [
     *               'products' 		=> $products, 				# 产品详细信息,详情参看代码中的$products。
     *               'product_total' => $product_total, 			# 产品的当前货币总额
     *               'base_product_total' => $base_product_total,# 产品的基础货币总额
     *               'product_weight'=> $product_weight,			# 蟾皮的总重量、
     *               ]
     *               得到当前购物车的产品信息,具体参看上面的example array。
     */
232
    public function getCartProductInfo($activeProduct = true)
_
__FresHmaN 已提交
233
    {
T
Terry 已提交
234 235 236
        $cart_id        = Yii::$service->cart->quote->getCartId();
        $products       = [];
        $product_total  = 0;
_
__FresHmaN 已提交
237
        $product_weight = 0;
238
        $product_volume_weight = 0;
239 240 241
        $base_product_total = 0;
        $product_volume = 0;
        $product_qty_total = 0;
_
__FresHmaN 已提交
242 243
        if ($cart_id) {
            if (!isset($this->_cart_product_info[$cart_id])) {
T
Terry 已提交
244
                $data = $this->_itemModel->find()->where([
_
__FresHmaN 已提交
245 246 247 248
                    'cart_id' => $cart_id,
                ])->all();
                if (is_array($data) && !empty($data)) {
                    foreach ($data as $one) {
249 250 251 252
                        $active             = $one['active'];
                        if ($activeProduct && ($active != $this->activeStatus)) {
                            continue;
                        }
T
Terry 已提交
253 254
                        $product_id     = $one['product_id'];
                        $product_one    = Yii::$service->product->getByPrimaryKey($product_id);
_
__FresHmaN 已提交
255
                        if ($product_one['_id']) {
T
Terry 已提交
256
                            $qty                = $one['qty'];
257
                            
T
Terry 已提交
258 259
                            $custom_option_sku  = $one['custom_option_sku'];
                            $product_price_arr  = Yii::$service->product->price->getCartPriceByProductId($product_id, $qty, $custom_option_sku, 2);
_
__FresHmaN 已提交
260 261
                            $curr_product_price = isset($product_price_arr['curr_price']) ? $product_price_arr['curr_price'] : 0;
                            $base_product_price = isset($product_price_arr['base_price']) ? $product_price_arr['base_price'] : 0;
T
Terry 已提交
262
                            $product_price      = isset($curr_product_price['value']) ? $curr_product_price['value'] : 0;
_
__FresHmaN 已提交
263

T
Terry 已提交
264
                            $product_row_price  = $product_price * $qty;
_
__FresHmaN 已提交
265
                            $base_product_row_price = $base_product_price * $qty;
266
                            
267
                            $volume = Yii::$service->shipping->getVolume($product_one['long'], $product_one['width'], $product_one['high']);
268
                            $p_pv               = $volume * $qty;
T
Terry 已提交
269
                            $p_wt               = $product_one['weight'] * $qty;
270
                            $p_vwt              = $product_one['volume_weight'] * $qty;
271 272
                            
                            if ($active == $this->activeStatus) {
273
                                $product_total          += $product_row_price;
274 275 276 277
                                $base_product_total     += $base_product_row_price;
                                $product_weight         += $p_wt;
                                $product_volume_weight  += $p_vwt;
                                $product_volume         += $p_pv;
278
                                $product_qty_total      += $qty;
279
                            }
T
Terry 已提交
280
                            $productSpuOptions  = $this->getProductSpuOptions($product_one);
_
__FresHmaN 已提交
281
                            $products[] = [
T
Terry 已提交
282
                                'item_id'           => $one['item_id'],
283
                                'active'            => $active,
_
__FresHmaN 已提交
284
                                'product_id'        => $product_id,
T
Terry 已提交
285 286 287
                                'sku'               => $product_one['sku'],
                                'name'              => Yii::$service->store->getStoreAttrVal($product_one['name'], 'name'),
                                'qty'               => $qty,
_
__FresHmaN 已提交
288
                                'custom_option_sku' => $custom_option_sku,
T
Terry 已提交
289
                                'product_price'     => $product_price,
_
__FresHmaN 已提交
290 291 292
                                'product_row_price' => $product_row_price,

                                'base_product_price'    => $base_product_price,
T
Terry 已提交
293
                                'base_product_row_price'=> $base_product_row_price,
_
__FresHmaN 已提交
294

T
Terry 已提交
295
                                'product_name'      => $product_one['name'],
_
__FresHmaN 已提交
296 297
                                'product_weight'    => $product_one['weight'],
                                'product_row_weight'=> $p_wt,
298 299 300 301
                                'product_volume_weight'     => $product_one['volume_weight'],
                                'product_row_volume_weight' => $p_vwt,
                                'product_volume'        => $volume,
                                'product_row_volume'    => $p_pv,
T
Terry 已提交
302 303 304 305
                                'product_url'       => $product_one['url_key'],
                                'product_image'     => $product_one['image'],
                                'custom_option'     => $product_one['custom_option'],
                                'spu_options'       => $productSpuOptions,
_
__FresHmaN 已提交
306 307 308 309
                            ];
                        }
                    }
                    $this->_cart_product_info[$cart_id] = [
310 311 312 313 314 315 316
                        'products'              => $products,
                        'product_qty_total'     => $product_qty_total,
                        'product_total'         => $product_total,
                        'base_product_total'    => $base_product_total,
                        'product_weight'        => $product_weight,
                        'product_volume_weight' => $product_volume_weight,
                        'product_volume'        => $product_volume,
317
                        
_
__FresHmaN 已提交
318 319 320 321 322 323 324 325 326
                    ];
                }
            }

            return $this->_cart_product_info[$cart_id];
        }
    }

    /**
T
Terry 已提交
327
     * @param $productOb | Object,类型:\fecshop\models\mongodb\Product
_
__FresHmaN 已提交
328 329 330 331 332 333 334 335 336 337 338
     * 得到产品的spu对应的属性以及值。
     * 概念 - spu options:当多个产品是同一个spu,但是不同的sku的时候,他们的产品表里面的
     * spu attr 的值是不同的,譬如对应鞋子,size 和 color 就是spu attr,对于同一款鞋子,他们
     * 是同一个spu,对于尺码,颜色不同的鞋子,是不同的sku,他们的spu attr 就是 color 和 size。
     */
    protected function getProductSpuOptions($productOb)
    {
        $custom_option_info_arr = [];
        if (isset($productOb['attr_group']) && !empty($productOb['attr_group'])) {
            $productAttrGroup = $productOb['attr_group'];
            Yii::$service->product->addGroupAttrs($productAttrGroup);
T
Terry 已提交
339 340
            $productOb  = Yii::$service->product->getByPrimaryKey((string) $productOb['_id']);
            $spuArr     = Yii::$service->product->getSpuAttr($productAttrGroup);
_
__FresHmaN 已提交
341 342 343 344 345 346 347 348 349 350 351 352 353
            if (is_array($spuArr) && !empty($spuArr)) {
                foreach ($spuArr as $spu_attr) {
                    if (isset($productOb[$spu_attr]) && !empty($productOb[$spu_attr])) {
                        $custom_option_info_arr[$spu_attr] = $productOb[$spu_attr];
                    }
                }
            }
        }

        return $custom_option_info_arr;
    }

    /**
T
Terry 已提交
354
     * @param $item_id | Int , quoteItem表的id
_
__FresHmaN 已提交
355 356 357 358 359 360 361
     * @return bool
     *              将这个item_id对应的产品个数+1.
     */
    public function addOneItem($item_id)
    {
        $cart_id = Yii::$service->cart->quote->getCartId();
        if ($cart_id) {
T
Terry 已提交
362
            $one = $this->_itemModel->find()->where([
_
__FresHmaN 已提交
363 364 365
                'cart_id' => $cart_id,
                'item_id' => $item_id,
            ])->one();
T
Terry 已提交
366 367 368 369 370
            $product_id = $one['product_id'];
            if ($one['item_id'] && $product_id) {
                $product = Yii::$service->product->getByPrimaryKey($product_id);
                $changeQty = Yii::$service->cart->getCartQty($product['package_number'], 1);
                $one['qty'] = $one['qty'] + $changeQty;
_
__FresHmaN 已提交
371 372 373
                $one->save();
                // 重新计算购物车的数量
                Yii::$service->cart->quote->computeCartInfo();
374 375 376 377 378
                $item = [
                    'product_id' 		=> $product_id,
                    'custom_option_sku' => $one['custom_option_sku'],
                    'qty' 				=> $changeQty,
                    'sku' 				=> $product['sku'],
T
Terry 已提交
379
                    'afterAddQty'       => $one['qty'],
380 381
                ];
                // 购物车数据加1
T
Terry 已提交
382
                $this->sendTraceAddToCartInfoByApi($item);
_
__FresHmaN 已提交
383 384 385 386 387 388 389 390
                return true;
            }
        }

        return false;
    }

    /**
T
Terry 已提交
391
     * @param $item_id | Int , quoteItem表的id
_
__FresHmaN 已提交
392 393 394 395 396 397 398
     * @return bool
     *              将这个item_id对应的产品个数-1.
     */
    public function lessOneItem($item_id)
    {
        $cart_id = Yii::$service->cart->quote->getCartId();
        if ($cart_id) {
T
Terry 已提交
399
            $one = $this->_itemModel->find()->where([
_
__FresHmaN 已提交
400 401 402
                'cart_id' => $cart_id,
                'item_id' => $item_id,
            ])->one();
T
Terry 已提交
403
            $product_id = $one['product_id'];
404
            $product = Yii::$service->product->getByPrimaryKey($one['product_id']);
T
Terry 已提交
405 406 407 408 409
            $changeQty = Yii::$service->cart->getCartQty($product['package_number'], 1);
            $lessedQty = $one['qty'] - $changeQty;
            $min_sales_qty = 1;
            if ($product['min_sales_qty'] && $product['min_sales_qty'] >= 2) {
                $min_sales_qty = $product['min_sales_qty'];
410
            }
S
Success Gao 已提交
411
            if ($lessedQty < $min_sales_qty) {
T
fix bug  
Terry 已提交
412
                Yii::$service->helper->errors->add('product less buy qty is {min_sales_qty}', ['min_sales_qty' => $product['min_sales_qty']]);
T
Terry 已提交
413 414 415 416
                
                return false;
            }
            
_
__FresHmaN 已提交
417 418
            if ($one['item_id']) {
                if ($one['qty'] > 1) {
419
                    $one['qty'] = $lessedQty;
_
__FresHmaN 已提交
420 421 422 423 424 425 426 427 428 429 430 431 432
                    $one->save();
                    // 重新计算购物车的数量
                    Yii::$service->cart->quote->computeCartInfo();

                    return true;
                }
            }
        }

        return false;
    }

    /**
T
Terry 已提交
433
     * @param $item_id | Int , quoteItem表的id
_
__FresHmaN 已提交
434 435 436 437 438 439 440
     * @return bool
     *              将这个item_id对应的产品删除
     */
    public function removeItem($item_id)
    {
        $cart_id = Yii::$service->cart->quote->getCartId();
        if ($cart_id) {
T
Terry 已提交
441
            $one = $this->_itemModel->find()->where([
_
__FresHmaN 已提交
442 443 444 445 446 447 448 449 450 451 452 453 454 455
                'cart_id' => $cart_id,
                'item_id' => $item_id,
            ])->one();
            if ($one['item_id']) {
                $one->delete();
                // 重新计算购物车的数量
                Yii::$service->cart->quote->computeCartInfo();

                return true;
            }
        }

        return false;
    }
456 457
    
    /**
T
Terry 已提交
458
     * @param $item_id | Int , quoteItem表的id
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
     * @return bool
     *              将这个item_id对应的产品个数+1.
     */
    public function selectOneItem($item_id, $checked)
    {
        $cart_id = Yii::$service->cart->quote->getCartId();
        if ($cart_id) {
            $one = $this->_itemModel->find()->where([
                'cart_id' => $cart_id,
                'item_id' => $item_id,
            ])->one();
            $product_id = $one['product_id'];
            if ($one['item_id'] && $product_id) {
                //$product = Yii::$service->product->getByPrimaryKey($product_id);
                //$changeQty = Yii::$service->cart->getCartQty($product['package_number'], 1);
                //$one['qty'] = $one['qty'] + $changeQty;
                if ($checked == true) {
                    $one->active = $this->activeStatus;
                } else {
                    $one->active = $this->noActiveStatus;
                }
                $one->save();
                // 重新计算购物车的数量
                Yii::$service->cart->quote->computeCartInfo();

                return true;
            }
        }

        return false;
    }
    
    /**
T
Terry 已提交
492
     * @param $item_id | Int , quoteItem表的id
493 494 495 496 497 498 499 500 501 502 503 504 505 506
     * @return bool
     *              将这个item_id对应的产品个数+1.
     */
    public function selectAllItem($checked)
    {
        $cart_id = Yii::$service->cart->quote->getCartId();
        if ($cart_id) {
            $active = $this->noActiveStatus;
            if ($checked == true) {
                $active = $this->activeStatus;
            }
            $updateCount = $this->_itemModel->updateAll(
                ['active'  => $active],
                ['cart_id' => $cart_id]
S
Success Gao 已提交
507
            );
508 509 510 511 512 513 514 515 516 517
            if ($updateCount > 0) {
                Yii::$service->cart->quote->computeCartInfo();
            }
            
            return true;
        }

        return false;
    }
    
S
Success Gao 已提交
518
    /**
T
Terry 已提交
519
     * @param $cart_id | int 购物车id
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
     * 删除购物车中的所有的active产品。对于noActive产品保留
     * 注意:清空购物车并不是清空所有信息,仅仅是清空用户购物车中的产品。
     * 另外,购物车的数目更改后,需要更新cart中产品个数的信息。
     */
    public function removeNoActiveItemsByCartId($cart_id = '')
    {
        if (!$cart_id) {
            $cart_id = Yii::$service->cart->quote->getCartId();
        }
        if ($cart_id) {
            $columns = $this->_itemModel->deleteAll([
                'cart_id' => $cart_id,
                'active'  => $this->activeStatus,
            ]);
            if ($columns > 0) {
                // 重新计算购物车的数量
                Yii::$service->cart->quote->computeCartInfo();
_
__FresHmaN 已提交
537

538 539 540 541 542 543
                return true;
            }
        }
    }

    /** 废弃,改为 removeNoActiveItemsByCartId(),因为购物车改为勾选下单方式。
T
Terry 已提交
544
     * @param $cart_id | int 购物车id
_
__FresHmaN 已提交
545 546 547 548 549 550 551 552 553 554
     * 删除购物车中的所有产品。
     * 注意:清空购物车并不是清空所有信息,仅仅是清空用户购物车中的产品。
     * 另外,购物车的数目更改后,需要更新cart中产品个数的信息。
     */
    public function removeItemByCartId($cart_id = '')
    {
        if (!$cart_id) {
            $cart_id = Yii::$service->cart->quote->getCartId();
        }
        if ($cart_id) {
T
Terry 已提交
555
            $items = $this->_itemModel->deleteAll([
_
__FresHmaN 已提交
556 557 558 559 560 561 562 563 564 565 566
                'cart_id' => $cart_id,
                //'item_id' => $item_id,
            ]);
            // 重新计算购物车的数量
            Yii::$service->cart->quote->computeCartInfo(0);
        }

        return true;
    }

    /**
T
Terry 已提交
567 568
     * @param $new_cart_id | int 更新后的cart_id
     * @param $cart_id | int 更新前的cart_id
_
__FresHmaN 已提交
569 570 571 572 573 574
     * 删除购物车中的所有产品。
     * 这里仅仅更改cart表的cart_id, 而不会做其他任何事情。
     */
    public function updateCartId($new_cart_id, $cart_id)
    {
        if ($cart_id && $new_cart_id) {
T
Terry 已提交
575
            $this->_itemModel->updateAll(
576 577
                ['cart_id' => $new_cart_id],  // $attributes
                ['cart_id' => $cart_id]       // $condition
_
__FresHmaN 已提交
578 579 580 581 582 583 584 585 586
            );
            // 重新计算购物车的数量
            //Yii::$service->cart->quote->computeCartInfo();
            return true;
        }

        return false;
    }
}