Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed cart duplication conditions at user login/logout #1801

Merged
merged 3 commits into from Jan 7, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
91 changes: 51 additions & 40 deletions core/lib/Thelia/Action/Cart.php
Expand Up @@ -251,8 +251,14 @@ protected function updateQuantity(EventDispatcherInterface $dispatcher, CartItem
*
* @return CartItem
*/
protected function doAddItem(EventDispatcherInterface $dispatcher, CartModel $cart, $productId, ProductSaleElements $productSaleElements, $quantity, ProductPriceTools $productPrices)
{
protected function doAddItem(
EventDispatcherInterface $dispatcher,
CartModel $cart,
$productId,
ProductSaleElements $productSaleElements,
$quantity,
ProductPriceTools $productPrices
) {
$cartItem = new CartItem();
$cartItem->setDisptacher($dispatcher);
$cartItem
Expand Down Expand Up @@ -342,31 +348,14 @@ public function restoreCurrentCart(CartRestoreEvent $cartRestoreEvent)
* @throws \Exception
* @throws \Propel\Runtime\Exception\PropelException
*/
private function manageNonPersistentCookie(CartRestoreEvent $cartRestoreEvent)
protected function manageNonPersistentCookie(CartRestoreEvent $cartRestoreEvent)
{
$cart = $cartRestoreEvent->getCart();

if (null === $cart) {
$cart = $cart = $this->dispatchNewCart($cartRestoreEvent->getDispatcher());
$cart = $this->dispatchNewCart($cartRestoreEvent->getDispatcher());
} else {
if (null !== $customer = $this->session->getCustomerUser()) {
// A customer is logged in.
if (null === $cart->getCustomerId()) {
// The cart created by the customer when it was not yet logged in
// is assigned to it after login.
$cart->setCustomerId($customer->getId())->save();
} elseif ($cart->getCustomerId() != $customer->getId()) {
// The cart does not belongs to the current customer
// -> clone it to create a new cart.
$cart = $this->duplicateCart(
$cartRestoreEvent->getDispatcher(),
$cart,
CustomerQuery::create()->findPk($customer->getId())
);
}
} else {
$cart->setCustomerId(null)->save();
}
$cart = $this->manageCartDuplicationAtCustomerLogin($cart, $cartRestoreEvent->getDispatcher());
}

return $cart;
Expand All @@ -382,31 +371,53 @@ private function manageNonPersistentCookie(CartRestoreEvent $cartRestoreEvent)
* @throws \Exception
* @throws \Propel\Runtime\Exception\PropelException
*/
private function managePersistentCart(CartRestoreEvent $cartRestoreEvent, $cookieName)
protected function managePersistentCart(CartRestoreEvent $cartRestoreEvent, $cookieName)
{
// The cart cookie exists -> get the cart token
$token = $this->request->cookies->get($cookieName);

// Check if a cart exists for this token
if (null !== $cart = CartQuery::create()->findOneByToken($token)) {
if (null !== $customer = $this->session->getCustomerUser()) {
// A customer is logged in.
if (null === $cart->getCustomerId()) {
// The cart created by the customer when it was not yet logged in
// is assigned to it after login.
$cart->setCustomerId($customer->getId())->save();
} elseif ($cart->getCustomerId() != $customer->getId()) {
// The cart does not belongs to the current customer
// -> clone it to create a new cart.
$cart = $this->duplicateCart(
$cartRestoreEvent->getDispatcher(),
$cart,
CustomerQuery::create()->findPk($customer->getId())
);
$cart = $this->manageCartDuplicationAtCustomerLogin($cart, $cartRestoreEvent->getDispatcher());
}

return $cart;
}

protected function manageCartDuplicationAtCustomerLogin(CartModel $cart, EventDispatcherInterface $dispatcher)
{
/** @var CustomerModel $customer */
if (null !== $customer = $this->session->getCustomerUser()) {
// Check if we have to duplicate the existing cart.

$duplicateCart = true;

// A customer is logged in.
if (null === $cart->getCustomerId()) {
// If the customer has a discount, whe have to duplicate the cart,
// so that the discount will be applied to the products in cart.

if (0 === $customer->getDiscount() || 0 === $cart->countCartItems()) {
// If no discount, or an empty cart, there's no need to duplicate.
$duplicateCart = false;
}
} elseif ($cart->getCustomerId() != null) {
// Just duplicate the current cart, without assigning a customer ID.
$cart = $this->duplicateCart($cartRestoreEvent->getDispatcher(), $cart);
}

if ($duplicateCart) {
// Duplicate the cart
$cart = $this->duplicateCart($dispatcher, $cart, $customer);
} else {
// No duplication required, just assign the cart to the customer
$cart->setCustomerId($customer->getId())->save();
}
} elseif ($cart->getCustomerId() != null) {
// The cart belongs to another user
if (0 === $cart->countCartItems()) {
// No items in cart, assign it to nobody.
$cart->setCustomerId(null)->save();
} else {
// Some itemls in cart, duplicate it without assigning a customer ID.
$cart = $this->duplicateCart($dispatcher, $cart);
}
}

Expand Down