From 84e8ca47d2cba3e8c73958a790d8ba341353331d Mon Sep 17 00:00:00 2001 From: Franck Allimant Date: Thu, 26 Nov 2015 11:01:27 +0100 Subject: [PATCH 1/3] Fixed cart duplication conditions at user login/logout --- core/lib/Thelia/Action/Cart.php | 85 +++++++++++++++++++-------------- 1 file changed, 48 insertions(+), 37 deletions(-) diff --git a/core/lib/Thelia/Action/Cart.php b/core/lib/Thelia/Action/Cart.php index 69c41ae8ac..f0092b482f 100644 --- a/core/lib/Thelia/Action/Cart.php +++ b/core/lib/Thelia/Action/Cart.php @@ -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 @@ -349,24 +355,7 @@ private function manageNonPersistentCookie(CartRestoreEvent $cartRestoreEvent) if (null === $cart) { $cart = $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; @@ -389,24 +378,46 @@ private function managePersistentCart(CartRestoreEvent $cartRestoreEvent, $cooki // 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; + } + + private 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); } } From 709a9ef97f421ef23079c2034bab38ddb1c4e20b Mon Sep 17 00:00:00 2001 From: Franck Allimant Date: Thu, 26 Nov 2015 11:16:29 +0100 Subject: [PATCH 2/3] fixed typo --- core/lib/Thelia/Action/Cart.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/lib/Thelia/Action/Cart.php b/core/lib/Thelia/Action/Cart.php index f0092b482f..a1e1f41585 100644 --- a/core/lib/Thelia/Action/Cart.php +++ b/core/lib/Thelia/Action/Cart.php @@ -353,7 +353,7 @@ private function manageNonPersistentCookie(CartRestoreEvent $cartRestoreEvent) $cart = $cartRestoreEvent->getCart(); if (null === $cart) { - $cart = $cart = $this->dispatchNewCart($cartRestoreEvent->getDispatcher()); + $cart = $this->dispatchNewCart($cartRestoreEvent->getDispatcher()); } else { $cart = $this->manageCartDuplicationAtCustomerLogin($cart, $cartRestoreEvent->getDispatcher()); } From 051614c2a316a9d4ec7cfd6dff1193e7127c2f31 Mon Sep 17 00:00:00 2001 From: Franck Allimant Date: Thu, 26 Nov 2015 11:17:23 +0100 Subject: [PATCH 3/3] Changed visibility of cart duplication methods to protected --- core/lib/Thelia/Action/Cart.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core/lib/Thelia/Action/Cart.php b/core/lib/Thelia/Action/Cart.php index a1e1f41585..6cb8645a71 100644 --- a/core/lib/Thelia/Action/Cart.php +++ b/core/lib/Thelia/Action/Cart.php @@ -348,7 +348,7 @@ 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(); @@ -371,7 +371,7 @@ 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); @@ -384,7 +384,7 @@ private function managePersistentCart(CartRestoreEvent $cartRestoreEvent, $cooki return $cart; } - private function manageCartDuplicationAtCustomerLogin(CartModel $cart, EventDispatcherInterface $dispatcher) + protected function manageCartDuplicationAtCustomerLogin(CartModel $cart, EventDispatcherInterface $dispatcher) { /** @var CustomerModel $customer */ if (null !== $customer = $this->session->getCustomerUser()) {