示例#1
0
文件: Cart.php 项目: margery/thelia
 /**
  *
  * The cart token is saved in a cookie so we try to retrieve it. Then the customer is checked.
  *
  * @param CartRestoreEvent $cartRestoreEvent
  * @param $cookieName
  * @return CartModel
  * @throws \Exception
  * @throws \Propel\Runtime\Exception\PropelException
  */
 private 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()));
             }
         } elseif ($cart->getCustomerId() != null) {
             // Just duplicate the current cart, without assigning a customer ID.
             $cart = $this->duplicateCart($cartRestoreEvent->getDispatcher(), $cart);
         }
     }
     return $cart;
 }
示例#2
0
 /**
  * Return the cart stored in the current session
  *
  * @param EventDispatcherInterface $dispatcher the event dispatcher, required if no cart is currently stored in the session
  *
  * @return Cart The cart in the current session.
  */
 public function getSessionCart(EventDispatcherInterface $dispatcher = null)
 {
     $cart_id = $this->get("thelia.cart_id", null);
     if (null !== $cart_id) {
         $cart = CartQuery::create()->findPk($cart_id);
     } else {
         $cart = self::$transientCart;
     }
     // If we do not have a cart, or if the current cart is nor valid
     // restore it from the cart cookie, or create a new one
     if (null === $cart || !$this->isValidCart($cart)) {
         // A dispatcher is required here. If we do not have it, throw an exception
         // This is a temporary workaround to ensure backward compatibility with getCart(),
         // When genCart() will be removed, this check should be removed, and  $dispatcher should become
         // a required parameter.
         if (null == $dispatcher) {
             throw new \InvalidArgumentException("In this context (no cart in session), an EventDispatcher should be provided to Session::getSessionCart().");
         }
         $cartEvent = new CartRestoreEvent();
         if (null !== $cart) {
             $cartEvent->setCart($cart);
         }
         $dispatcher->dispatch(TheliaEvents::CART_RESTORE_CURRENT, $cartEvent);
         if (null === ($cart = $cartEvent->getCart())) {
             throw new \LogicException("Unable to get a Cart.");
         }
         // Store the cart.
         $this->setSessionCart($cart);
     }
     return $cart;
 }
示例#3
0
 /**
  *
  * The cart token is saved in a cookie so we try to retrieve it. Then the customer is checked.
  *
  * @param CartRestoreEvent $cartRestoreEvent
  * @param $cookieName
  * @return CartModel
  * @throws \Exception
  * @throws \Propel\Runtime\Exception\PropelException
  */
 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))) {
         $cart = $this->manageCartDuplicationAtCustomerLogin($cart, $cartRestoreEvent->getDispatcher());
     }
     return $cart;
 }
示例#4
0
 /**
  * The cart token is not saved in a cookie, if the cart is present in session, we just change the customer id
  * if needed or create duplicate the current cart if the customer is not the same as customer already present in
  * the cart.
  *
  * @param CartRestoreEvent $cartRestoreEvent
  * @param EventDispatcherInterface $dispatcher
  * @return CartModel
  * @throws \Exception
  * @throws \Propel\Runtime\Exception\PropelException
  */
 protected function manageNonPersistentCookie(CartRestoreEvent $cartRestoreEvent, EventDispatcherInterface $dispatcher)
 {
     $cart = $cartRestoreEvent->getCart();
     if (null === $cart) {
         $cart = $this->dispatchNewCart($dispatcher);
     } else {
         $cart = $this->manageCartDuplicationAtCustomerLogin($cart, $dispatcher);
     }
     return $cart;
 }