/** * @param \Thelia\Core\HttpFoundation\Session\Session $session * @return \Thelia\Model\Cart */ protected function createCart(Session $session) { $cart = new CartModel(); $cart->setToken($this->generateCookie($session)); $cart->setCurrency($session->getCurrency(true)); if (null !== ($customer = $session->getCustomerUser())) { $cart->setCustomer($customer); } $cart->save(); $session->setCart($cart->getId()); return $cart; }
/** * Duplicate the current existing cart. Only the token is changed * * @param string $token * @param Customer $customer * @param Currency $currency * @param EventDispatcherInterface $dispatcher * @return Cart * @throws \Exception * @throws \Propel\Runtime\Exception\PropelException */ public function duplicate($token, Customer $customer = null, Currency $currency = null, EventDispatcherInterface $dispatcher = null) { if (!$dispatcher) { return false; } $cartItems = $this->getCartItems(); $cart = new Cart(); $cart->setAddressDeliveryId($this->getAddressDeliveryId()); $cart->setAddressInvoiceId($this->getAddressInvoiceId()); $cart->setToken($token); $discount = 0; if (null === $currency) { $currencyQuery = CurrencyQuery::create(); $currency = $currencyQuery->findPk($this->getCurrencyId()) ?: $currencyQuery->findOneByByDefault(1); } $cart->setCurrency($currency); if ($customer) { $cart->setCustomer($customer); if ($customer->getDiscount() > 0) { $discount = $customer->getDiscount(); } } $cart->save(); foreach ($cartItems as $cartItem) { $product = $cartItem->getProduct(); $productSaleElements = $cartItem->getProductSaleElements(); if ($product && $productSaleElements && $product->getVisible() == 1 && ($productSaleElements->getQuantity() >= $cartItem->getQuantity() || $product->getVirtual() === 1 || !ConfigQuery::checkAvailableStock())) { $item = new CartItem(); $item->setCart($cart); $item->setProductId($cartItem->getProductId()); $item->setQuantity($cartItem->getQuantity()); $item->setProductSaleElements($productSaleElements); $prices = $productSaleElements->getPricesByCurrency($currency, $discount); $item->setPrice($prices->getPrice())->setPromoPrice($prices->getPromoPrice())->setPromo($productSaleElements->getPromo()); $item->save(); $dispatcher->dispatch(TheliaEvents::CART_ITEM_DUPLICATE, new CartItemDuplicationItem($item, $cartItem)); } } try { $this->delete(); } catch (\Exception $e) { // just fail silently in some cases } return $cart; }
/** * Create a new, empty cart object, and assign it to the current customer, if any. * * @param CartCreateEvent $cartCreateEvent */ public function createEmptyCart(CartCreateEvent $cartCreateEvent) { $cart = new CartModel(); $cart->setCurrency($this->session->getCurrency(true)); if (null !== ($customer = $this->session->getCustomerUser())) { $cart->setCustomer(CustomerQuery::create()->findPk($customer->getId())); } $cartCreateEvent->setCart($cart); }
/** * Create a new, empty cart object, and assign it to the current customer, if any. * * @param CartCreateEvent $cartCreateEvent */ public function createEmptyCart(CartCreateEvent $cartCreateEvent) { $cart = new CartModel(); $cart->setCurrency($this->session->getCurrency(true)); if (null !== ($customer = $this->session->getCustomerUser())) { $cart->setCustomer(CustomerQuery::create()->findPk($customer->getId())); } $this->session->setSessionCart($cart); if (ConfigQuery::read("cart.use_persistent_cookie", 1) == 1) { // set cart_use_cookie to "" to remove the cart cookie // see Thelia\Core\EventListener\ResponseListener $this->session->set("cart_use_cookie", ""); } $cartCreateEvent->setCart($cart); }
/** * cart and customer already exists. Cart and customer are linked. * * cart in session must be return */ public function testGetCartWithExistingCartAndCustomer() { $cartTrait = $this->cartTrait; $request = $this->request; //create a fake customer just for test. If not persists test fails ! $customer = new Customer(); $customer->setFirstname("john"); $customer->setLastname("doe"); $customer->setTitleId(1); $customer->save(); $uniqid = uniqid("test2", true); //create a fake cart in database; $cart = new Cart(); $cart->setToken($uniqid); $cart->setCustomer($customer); $cart->save(); $request->cookies->set("thelia_cart", $uniqid); $request->getSession()->setCustomerUser($customer); $getCart = $cartTrait->getCart($this->dispatcher, $request); $this->assertInstanceOf("Thelia\\Model\\Cart", $getCart, '$cart must be an instance of cart model Thelia\\Model\\Cart'); $this->assertNotNull($getCart->getCustomerId()); $this->assertNull($getCart->getAddressDeliveryId()); $this->assertNull($getCart->getAddressInvoiceId()); $this->assertEquals($cart->getToken(), $getCart->getToken(), "token must be the same"); $this->assertEquals($customer->getId(), $getCart->getCustomerId()); }