/** * @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; }
public function fillCart() { $currency = CurrencyQuery::create()->findOne(); //create a fake cart in database; $cart = new Cart(); $cart->setToken(uniqid("createorder", true))->setCustomer($this->customer)->setCurrency($currency)->save(); /* add 3 items */ $productList = array(); for ($i = 0; $i < 3; $i++) { $pse = ProductSaleElementsQuery::create()->filterByProduct(ProductQuery::create()->filterByVisible(1)->filterById($productList, Criteria::NOT_IN)->find())->filterByQuantity(5, Criteria::GREATER_EQUAL)->joinProductPrice('pp', Criteria::INNER_JOIN)->addJoinCondition('pp', 'currency_id = ?', $currency->getId(), null, \PDO::PARAM_INT)->withColumn('`pp`.price', 'price_PRICE')->withColumn('`pp`.promo_price', 'price_PROMO_PRICE')->findOne(); $productList[] = $pse->getProductId(); $cartItem = new CartItem(); $cartItem->setCart($cart)->setProduct($pse->getProduct())->setProductSaleElements($pse)->setQuantity($i + 1)->setPrice($pse->getPrice())->setPromoPrice($pse->getPromoPrice())->setPromo($pse->getPromo())->setPriceEndOfLife(time() + 60 * 60 * 24 * 30)->save(); $this->cartItems[] = $cartItem; } $this->request->getSession()->setCart($cart->getId()); 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->setToken($this->generateCartCookieIdentifier())->setCurrency($this->session->getCurrency(true)); if (null !== ($customer = $this->session->getCustomerUser())) { $cart->setCustomer(CustomerQuery::create()->findPk($customer->getId())); } $cart->save(); $cartCreateEvent->setCart($cart); }
public function testGetCartWithExistingCartAndCustomerAndReferencesEachOther() { $session = $this->session; //create a fake customer just for test. If not persists test fails ! $customer = new Customer(); $customer->setFirstname("john test session"); $customer->setLastname("doe"); $customer->setTitleId(1); $customer->save(); $session->setCustomerUser($customer); $testCart = new Cart(); $testCart->setToken(uniqid("testSessionGetCart3", true)); $testCart->setCustomerId($customer->getId()); $testCart->save(); $this->request->cookies->set(ConfigQuery::read("cart.cookie_name", 'thelia_cart'), $testCart->getToken()); $cart = $session->getSessionCart($this->dispatcher); $this->assertNotNull($cart); $this->assertInstanceOf("\\Thelia\\Model\\Cart", $cart, '$cart must be an instance of Thelia\\Model\\Cart'); }
/** * Customer is connected but cart not associated to him * * A new cart must be created (duplicated) containing customer id */ public function testGetCartWithExistingCartAndCustomerButNotSameCustomerId() { $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("test3", true); //create a fake cart in database; $cart = new Cart(); $cart->setToken($uniqid); $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->assertNotEquals($cart->getToken(), $getCart->getToken(), "token must be different"); $this->assertEquals($customer->getId(), $getCart->getCustomerId()); }