public function parseResults(LoopResult $loopResult) { $taxCountry = $this->container->get('thelia.taxEngine')->getDeliveryCountry(); $locale = $this->request->getSession()->getLang()->getLocale(); $checkAvailability = ConfigQuery::checkAvailableStock(); $defaultAvailability = intval(ConfigQuery::read('default-available-stock', 100)); foreach ($loopResult->getResultDataCollection() as $cartItem) { $product = $cartItem->getProduct(null, $locale); $productSaleElement = $cartItem->getProductSaleElements(); $loopResultRow = new LoopResultRow(); $loopResultRow->set("ITEM_ID", $cartItem->getId()); $loopResultRow->set("TITLE", $product->getTitle()); $loopResultRow->set("REF", $product->getRef()); $loopResultRow->set("QUANTITY", $cartItem->getQuantity()); $loopResultRow->set("PRODUCT_ID", $product->getId()); $loopResultRow->set("PRODUCT_URL", $product->getUrl($this->request->getSession()->getLang()->getLocale())); if (!$checkAvailability || $product->getVirtual() === 1) { $loopResultRow->set("STOCK", $defaultAvailability); } else { $loopResultRow->set("STOCK", $productSaleElement->getQuantity()); } $loopResultRow->set("PRICE", $cartItem->getPrice())->set("PROMO_PRICE", $cartItem->getPromoPrice())->set("TAXED_PRICE", $cartItem->getTaxedPrice($taxCountry))->set("PROMO_TAXED_PRICE", $cartItem->getTaxedPromoPrice($taxCountry))->set("IS_PROMO", $cartItem->getPromo() === 1 ? 1 : 0); $loopResultRow->set("PRODUCT_SALE_ELEMENTS_ID", $productSaleElement->getId()); $loopResultRow->set("PRODUCT_SALE_ELEMENTS_REF", $productSaleElement->getRef()); $loopResult->addRow($loopResultRow); } return $loopResult; }
public function checkStock($value, ExecutionContextInterface $context) { $data = $context->getRoot()->getData(); if (null === $data["product_sale_elements_id"]) { $context->addViolationAt("quantity", Translator::getInstance()->trans("Invalid product_sale_elements")); } else { $productSaleElements = ProductSaleElementsQuery::create()->filterById($data["product_sale_elements_id"])->filterByProductId($data["product"])->findOne(); $product = $productSaleElements->getProduct(); if ($productSaleElements->getQuantity() < $value && $product->getVirtual() === 0 && ConfigQuery::checkAvailableStock()) { $context->addViolation(Translator::getInstance()->trans("quantity value is not valid")); } } }
public function addQuantity($value) { $currentQuantity = $this->getQuantity(); $newQuantity = $currentQuantity + $value; if (ConfigQuery::checkAvailableStock()) { $productSaleElements = $this->getProductSaleElements(); $product = $productSaleElements->getProduct(); if ($product->getVirtual() === 0) { if ($productSaleElements->getQuantity() < $newQuantity) { $newQuantity = $currentQuantity; } } } $this->setQuantity($newQuantity); return $this; }
/** * 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 pay() { /* check customer */ $this->checkAuth(); /* check cart count */ $this->checkCartNotEmpty(); /* check stock not empty */ if (true === ConfigQuery::checkAvailableStock()) { if (null !== ($response = $this->checkStockNotEmpty())) { return $response; } } /* check delivery address and module */ $this->checkValidDelivery(); /* check invoice address and payment module */ $this->checkValidInvoice(); $orderEvent = $this->getOrderEvent(); $this->getDispatcher()->dispatch(TheliaEvents::ORDER_PAY, $orderEvent); $placedOrder = $orderEvent->getPlacedOrder(); if (null !== $placedOrder && null !== $placedOrder->getId()) { /* order has been placed */ if ($orderEvent->hasResponse()) { return $orderEvent->getResponse(); } else { return $this->generateRedirectFromRoute('order.placed', [], ['order_id' => $orderEvent->getPlacedOrder()->getId()]); } } else { /* order has not been placed */ return $this->generateRedirectFromRoute('cart.view'); } }
/** * Update product quantity if new status is canceled or if old status is canceled. * * @param ModelOrder $order * @param $newStatus * @param $canceledStatus * @throws \Exception * @throws \Propel\Runtime\Exception\PropelException */ protected function updateQuantityForCanceledOrder(ModelOrder $order, $newStatus, $canceledStatus) { $orderProductList = $order->getOrderProducts(); /** @var OrderProduct $orderProduct */ foreach ($orderProductList as $orderProduct) { $productSaleElementsId = $orderProduct->getProductSaleElementsId(); /** @var ProductSaleElements $productSaleElements */ if (null !== ($productSaleElements = ProductSaleElementsQuery::create()->findPk($productSaleElementsId))) { if ($newStatus == $canceledStatus) { $productSaleElements->setQuantity($productSaleElements->getQuantity() + $orderProduct->getQuantity()); } else { /* check still in stock */ if ($orderProduct->getQuantity() > $productSaleElements->getQuantity() && true === ConfigQuery::checkAvailableStock()) { throw new TheliaProcessException($productSaleElements->getRef() . " : Not enough stock"); } $productSaleElements->setQuantity($productSaleElements->getQuantity() - $orderProduct->getQuantity()); } $productSaleElements->save(); } } }