示例#1
0
 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;
             }
         }
         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);
         }
     }
     return $cart;
 }
示例#2
0
 /**
  * Filter the query by a related \Thelia\Model\Cart object
  *
  * @param \Thelia\Model\Cart|ObjectCollection $cart  the related object to use as filter
  * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL
  *
  * @return ChildCustomerQuery The current query, for fluid interface
  */
 public function filterByCart($cart, $comparison = null)
 {
     if ($cart instanceof \Thelia\Model\Cart) {
         return $this->addUsingAlias(CustomerTableMap::ID, $cart->getCustomerId(), $comparison);
     } elseif ($cart instanceof ObjectCollection) {
         return $this->useCartQuery()->filterByPrimaryKeys($cart->getPrimaryKeys())->endUse();
     } else {
         throw new PropelException('filterByCart() only accepts arguments of type \\Thelia\\Model\\Cart or Collection');
     }
 }
示例#3
0
 /**
  * A cart is valid if its customer ID is the same as the current logged in user
  *
  * @param Cart $cart The cart to check
  *
  * @return bool true if the cart is valid, false otherwise
  */
 protected function isValidCart(Cart $cart)
 {
     $customer = $this->getCustomerUser();
     return null !== $customer && $cart->getCustomerId() == $customer->getId() || null === $customer && $cart->getCustomerId() === null;
 }
示例#4
0
 /**
  *
  *
  * @param  \Thelia\Model\Cart                     $cart
  * @throws \Thelia\Exception\InvalidCartException
  */
 protected function verifyValidCart(Cart $cart)
 {
     $customer = $this->getCustomerUser();
     if ($customer && $cart->getCustomerId() != $customer->getId()) {
         throw new InvalidCartException("customer in session and customer_id in cart are not the same");
     } elseif ($customer === null && $cart->getCustomerId() !== null) {
         throw new InvalidCartException("Customer exists in cart and not in session");
     }
 }