/** * 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; }
/** * * Refresh article's price * * @param \Thelia\Model\Cart $cart * @param \Thelia\Model\Currency $currency */ public function updateCartPrices(CartModel $cart, CurrencyModel $currency) { $customer = $cart->getCustomer(); $discount = 0; if (null !== $customer && $customer->getDiscount() > 0) { $discount = $customer->getDiscount(); } // cart item foreach ($cart->getCartItems() as $cartItem) { $productSaleElements = $cartItem->getProductSaleElements(); $productPrice = $productSaleElements->getPricesByCurrency($currency, $discount); $cartItem->setPrice($productPrice->getPrice())->setPromoPrice($productPrice->getPromoPrice()); $cartItem->save(); } // update the currency cart $cart->setCurrencyId($currency->getId()); $cart->save(); }
/** * 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()); }
/** * @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; }
public function import($startRecord = 0) { $count = 0; $errors = 0; $hdl = $this->t1db->query(sprintf("select * from commande order by id asc limit %d, %d", intval($startRecord), $this->getChunkSize())); while ($hdl && ($commande = $this->t1db->fetch_object($hdl))) { $count++; try { $this->order_corresp->getT2($commande->id); Tlog::getInstance()->warning("Order ID={$commande->id} already imported."); continue; } catch (ImportException $ex) { // Okay, the order was not imported. } try { if (null === ($customer = CustomerQuery::create()->findPk($this->cust_corresp->getT2($commande->client)))) { throw new ImportException("Failed to find customer ID={$commande->client}"); } if (null === ($status = OrderStatusQuery::create()->findPk($commande->statut))) { throw new ImportException("Failed to find order status ID={$commande->statut}"); } // Create invoice address if (false == ($adr_livr = $this->t1db->query_obj("select * from venteadr where id=?", array($commande->adrlivr)))) { throw new ImportException("Failed to find delivery adresse ID={$commande->adrlivr}"); } // Create invoice address if (false == ($adr_fact = $this->t1db->query_obj("select * from venteadr where id=?", array($commande->adrfact)))) { throw new ImportException("Failed to find billing adresse ID={$commande->adrfact}"); } $con = Propel::getConnection(OrderTableMap::DATABASE_NAME); $con->beginTransaction(); try { $order = new Order(); $delivery_adr = new OrderAddress(); $delivery_adr->setCustomerTitleId($this->getT2CustomerTitle($adr_livr->raison)->getId())->setCompany(isset($adr_livr->entreprise) ? $adr_livr->entreprise : '')->setFirstname($adr_livr->prenom)->setLastname($adr_livr->nom)->setAddress1($adr_livr->adresse1)->setAddress2($adr_livr->adresse2)->setAddress3($adr_livr->adresse3)->setZipcode($adr_livr->cpostal)->setCity($adr_livr->ville)->setPhone($adr_livr->tel)->setCountryId($this->getT2Country($adr_livr->pays)->getId())->save($con); $billing_adr = new OrderAddress(); $billing_adr->setCustomerTitleId($this->getT2CustomerTitle($adr_fact->raison)->getId())->setCompany(isset($adr_fact->entreprise) ? $adr_fact->entreprise : '')->setFirstname($adr_fact->prenom)->setLastname($adr_fact->nom)->setAddress1($adr_fact->adresse1)->setAddress2($adr_fact->adresse2)->setAddress3($adr_fact->adresse3)->setZipcode($adr_fact->cpostal)->setCity($adr_fact->ville)->setPhone($adr_fact->tel)->setCountryId($this->getT2Country($adr_fact->pays)->getId())->save($con); // Find the first availables delivery and payment modules, that's the best we can do. $deliveryModule = ModuleQuery::create()->findOneByType(BaseModule::DELIVERY_MODULE_TYPE); $paymentModule = ModuleQuery::create()->findOneByType(BaseModule::PAYMENT_MODULE_TYPE); // Create a cart (now required) $cart = new Cart(); $cart->save(); $order->setRef($commande->ref)->setCustomer($customer)->setInvoiceDate($commande->datefact)->setCurrency($this->getT2Currency($commande->devise))->setCurrencyRate($this->getT2Currency($commande->devise)->getRate())->setTransactionRef($commande->transaction)->setDeliveryRef($commande->livraison)->setInvoiceRef($commande->facture)->setPostage($commande->port)->setStatusId($status->getId())->setLang($this->getT2Lang($commande->lang))->setDeliveryOrderAddressId($delivery_adr->getId())->setInvoiceOrderAddressId($billing_adr->getId())->setDeliveryModuleId($deliveryModule->getId())->setPaymentModuleId($paymentModule->getId())->setDiscount($commande->remise)->setCartId($cart->getId())->save($con); // Update the order reference $order->setRef($commande->ref)->setCreatedAt($commande->date)->save(); if ($commande->remise > 0) { $coupon = new OrderCoupon(); $coupon->setOrder($order)->setCode('Not Available')->setType('UNKNOWN')->setAmount($commande->remise)->setTitle('Imported from Thelia 1')->setShortDescription('Imported from Thelia 1')->setDescription('Imported from Thelia 1')->setExpirationDate(time())->setIsCumulative(false)->setIsRemovingPostage(false)->setIsAvailableOnSpecialOffers(false)->setSerializedConditions(new ConditionCollection(array()))->save($con); } $vps = $this->t1db->query_list("select * from venteprod where commande=?", array($commande->id)); foreach ($vps as $vp) { $parent = 0; if (isset($vp->parent) && $vp->parent != 0) { $parent = $this->product_corresp->getT2($vp->parent); } $orderProduct = new OrderProduct(); $orderProduct->setOrder($order)->setProductRef($vp->ref)->setProductSaleElementsRef($vp->ref)->setTitle($vp->titre)->setChapo($vp->chapo)->setDescription($vp->description)->setPostscriptum("")->setQuantity($vp->quantite)->setPrice($vp->prixu / (1 + $vp->tva / 100))->setPromoPrice($vp->prixu / (1 + $vp->tva / 100))->setWasNew(false)->setWasInPromo(false)->setWeight(0)->setEanCode("")->setTaxRuleTitle("")->setTaxRuleDescription("")->setParent($parent)->save($con); $orderProductTax = new OrderProductTax(); $orderProductTax->setAmount($orderProduct->getPrice() * ($vp->tva / 100))->setPromoAmount(0)->setOrderProduct($orderProduct)->setTitle("TVA {$vp->tva} %")->setDescription("TVA {$vp->tva} %")->save($con); } $con->commit(); } catch (\Exception $ex) { $con->rollBack(); throw $ex; } } catch (\Exception $ex) { Tlog::getInstance()->addError("Failed to import order ref {$commande->ref}: ", $ex->getMessage()); $errors++; } } return new ImportChunkResult($count, $errors); }