diff --git a/core/lib/Thelia/Action/Cart.php b/core/lib/Thelia/Action/Cart.php index 49574c11c..b160c82fd 100755 --- a/core/lib/Thelia/Action/Cart.php +++ b/core/lib/Thelia/Action/Cart.php @@ -26,6 +26,10 @@ namespace Thelia\Action; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Thelia\Core\Event\ActionEvent; +use Thelia\Core\HttpFoundation\Session\Session; +use Thelia\Model\CartQuery; +use Thelia\Model\Cart as CartModel; +use Thelia\Model\Customer; class Cart implements EventSubscriberInterface @@ -94,6 +98,10 @@ class Cart implements EventSubscriberInterface public function getCart(Request $request) { + if (null !== $cartId = $request->getSession()->getCart()){ + $cart = CartQuery::create()->findPk($cartId); + } + if ($request->cookies->has("thelia_cart")) { //le cookie de panier existe, on le récupère $cookie = $request->cookies->get("thelia_cart"); @@ -117,19 +125,30 @@ class Cart implements EventSubscriberInterface } } else { - $cart = $this->createCart(); + $cart = $this->createCart($request->getSession()); } } else { //le cookie de panier n'existe pas, il va falloir le créer et faire un enregistrement en base. - $cart = $this->createCart(); + $cart = $this->createCart($request->getSession()); } return $cart; } - public function createCart() + public function createCart(Session $session) { + $cart = new CartModel(); + $cart->setToken($this->generateCookie()); + if(null !== $customer = $session->getCustomerUser()) { + $cart->setCustomer($customer); + } + + $cart->save(); + + $session->setCart($cart->getId()); + + return $cart; } diff --git a/core/lib/Thelia/Core/HttpFoundation/Session/Session.php b/core/lib/Thelia/Core/HttpFoundation/Session/Session.php index ac684f6c6..2d621fbf3 100755 --- a/core/lib/Thelia/Core/HttpFoundation/Session/Session.php +++ b/core/lib/Thelia/Core/HttpFoundation/Session/Session.php @@ -113,4 +113,16 @@ class Session extends BaseSession { return $this->get('return_to_url', URL::getIndexPage()); } + // -- Cart ------------------------------------------------------------------ + + public function getCart() + { + return $this->get("cart_id"); + } + + public function setCart($cart_id) + { + $this->set("cart_id", $cart_id); + } + }