/** * 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; }
/** * try to attach a new item to an existing cart * * @param EventDispatcherInterface $dispatcher * @param \Thelia\Model\Cart $cart * @param int $productId * @param ProductSaleElements $productSaleElements * @param float $quantity * @param ProductPriceTools $productPrices * * @return CartItem */ protected function doAddItem(EventDispatcherInterface $dispatcher, CartModel $cart, $productId, ProductSaleElements $productSaleElements, $quantity, ProductPriceTools $productPrices) { $cartItem = new CartItem(); $cartItem->setDisptacher($dispatcher); $cartItem->setCart($cart)->setProductId($productId)->setProductSaleElementsId($productSaleElements->getId())->setQuantity($quantity)->setPrice($productPrices->getPrice())->setPromoPrice($productPrices->getPromoPrice())->setPromo($productSaleElements->getPromo())->setPriceEndOfLife(time() + ConfigQuery::read("cart.priceEOF", 60 * 60 * 24 * 30))->save(); return $cartItem; }
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; }