已通过重写 Quote 服务里的 getCartId 方法实现了:登录用户只会有一个购物车。
代码如下:
public function getCartId()
{
if (!$this->_cart_id) {
$cart_id = Yii::$service->session->get(self::SESSION_CART_ID);
if (! $cart_id) {
if (! Yii::$app->user->isGuest) {
$customerId = Yii::$app->user->getId();
$cart = $this->getCartByCustomerId($customerId);
if ($cart && $cart['cart_id']) {
$this->setCartId($cart['cart_id']);
}
}
} else {
$this->_cart_id = $cart_id;
}
}
return $this->_cart_id;
}