/** * * search if cart already exists in session. If not try to create a new one or duplicate an old one. * * @param EventDispatcherInterface $dispatcher the event dispatcher * @param \Symfony\Component\HttpFoundation\Request $request * @return \Thelia\Model\Cart */ public function getCart(EventDispatcherInterface $dispatcher, Request $request) { $session = $request->getSession(); if (null !== ($cart = $session->getCart())) { return $cart; } if ($request->cookies->has("thelia_cart")) { //le cookie de panier existe, on le récupère $token = $request->cookies->get("thelia_cart"); $cart = CartQuery::create()->findOneByToken($token); if ($cart) { //le panier existe en base $customer = $session->getCustomerUser(); if ($customer) { if ($cart->getCustomerId() != $customer->getId()) { //le customer du panier n'est pas le mm que celui connecté, il faut cloner le panier sans le customer_id $cart = $this->duplicateCart($dispatcher, $cart, $session, $customer); } } else { if ($cart->getCustomerId() != null) { //il faut dupliquer le panier sans le customer_id $cart = $this->duplicateCart($dispatcher, $cart, $session); } } } else { $cart = $this->createCart($session); } } else { //le cookie de panier n'existe pas, il va falloir le créer et faire un enregistrement en base. $cart = $this->createCart($session); } $session->setCart($cart->getId()); return $cart; }
/** * * 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; }
/** * * 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; }
/** * If this collection has already been initialized with * an identical criteria, it returns the collection. * Otherwise if this Customer is new, it will return * an empty collection; or if this Customer has previously * been saved, it will retrieve related Carts from storage. * * This method is protected by default in order to keep the public * api reasonable. You can provide public methods for those you * actually need in Customer. * * @param Criteria $criteria optional Criteria object to narrow the query * @param ConnectionInterface $con optional connection object * @param string $joinBehavior optional join type to use (defaults to Criteria::LEFT_JOIN) * @return Collection|ChildCart[] List of ChildCart objects */ public function getCartsJoinCurrency($criteria = null, $con = null, $joinBehavior = Criteria::LEFT_JOIN) { $query = ChildCartQuery::create(null, $criteria); $query->joinWith('Currency', $joinBehavior); return $this->getCarts($query, $con); }
/** * Performs an INSERT on the database, given a Cart or Criteria object. * * @param mixed $criteria Criteria or Cart object containing data that is used to create the INSERT statement. * @param ConnectionInterface $con the ConnectionInterface connection to use * @return mixed The new primary key. * @throws PropelException Any exceptions caught during processing will be * rethrown wrapped into a PropelException. */ public static function doInsert($criteria, ConnectionInterface $con = null) { if (null === $con) { $con = Propel::getServiceContainer()->getWriteConnection(CartTableMap::DATABASE_NAME); } if ($criteria instanceof Criteria) { $criteria = clone $criteria; // rename for clarity } else { $criteria = $criteria->buildCriteria(); // build Criteria from Cart object } if ($criteria->containsKey(CartTableMap::ID) && $criteria->keyContainsValue(CartTableMap::ID)) { throw new PropelException('Cannot insert a value for auto-increment primary key (' . CartTableMap::ID . ')'); } // Set the correct dbName $query = CartQuery::create()->mergeWith($criteria); try { // use transaction because $criteria could contain info // for more than one table (I guess, conceivably) $con->beginTransaction(); $pk = $query->doInsert($con); $con->commit(); } catch (PropelException $e) { $con->rollBack(); throw $e; } return $pk; }
/** * If this collection has already been initialized with * an identical criteria, it returns the collection. * Otherwise if this Currency is new, it will return * an empty collection; or if this Currency has previously * been saved, it will retrieve related Carts from storage. * * This method is protected by default in order to keep the public * api reasonable. You can provide public methods for those you * actually need in Currency. * * @param Criteria $criteria optional Criteria object to narrow the query * @param ConnectionInterface $con optional connection object * @param string $joinBehavior optional join type to use (defaults to Criteria::LEFT_JOIN) * @return Collection|ChildCart[] List of ChildCart objects */ public function getCartsJoinAddressRelatedByAddressInvoiceId($criteria = null, $con = null, $joinBehavior = Criteria::LEFT_JOIN) { $query = ChildCartQuery::create(null, $criteria); $query->joinWith('AddressRelatedByAddressInvoiceId', $joinBehavior); return $this->getCarts($query, $con); }
/** * Removes this object from datastore and sets delete attribute. * * @param ConnectionInterface $con * @return void * @throws PropelException * @see Cart::setDeleted() * @see Cart::isDeleted() */ public function delete(ConnectionInterface $con = null) { if ($this->isDeleted()) { throw new PropelException("This object has already been deleted."); } if ($con === null) { $con = Propel::getServiceContainer()->getWriteConnection(CartTableMap::DATABASE_NAME); } $con->beginTransaction(); try { $deleteQuery = ChildCartQuery::create()->filterByPrimaryKey($this->getPrimaryKey()); $ret = $this->preDelete($con); if ($ret) { $deleteQuery->delete($con); $this->postDelete($con); $con->commit(); $this->setDeleted(true); } else { $con->commit(); } } catch (Exception $e) { $con->rollBack(); throw $e; } }
/** * 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; }
/** * Get the associated ChildCart object * * @param ConnectionInterface $con Optional Connection object. * @return ChildCart The associated ChildCart object. * @throws PropelException */ public function getCart(ConnectionInterface $con = null) { if ($this->aCart === null && $this->cart_id !== null) { $this->aCart = ChildCartQuery::create()->findPk($this->cart_id, $con); /* The following can be used additionally to guarantee the related object contains a reference to this object. This level of coupling may, however, be undesirable since it could result in an only partially populated collection in the referenced object. $this->aCart->addOrders($this); */ } return $this->aCart; }
/** * return cart if exists and is valid (checking customer) * * @return \Thelia\Model\Cart|null */ public function getCart() { $cart_id = $this->get("thelia.cart_id"); $cart = null; if ($cart_id) { $cart = CartQuery::create()->findPk($cart_id); if ($cart) { try { $this->verifyValidCart($cart); } catch (InvalidCartException $e) { $cart = null; } } } return $cart; }