public function trackCart(CartEvent $event, $eventName, EventDispatcherInterface $dispatcher) { $cart = $event->getCart(); foreach ($cart->getCartItems() as $cartItem) { $product = $cartItem->getProduct(); $defaultCategory = CategoryQuery::create()->findPk($product->getDefaultCategoryId()); $this->tracker->addEcommerceItem($product->getRef() ? $product->getRef() : $product->getId(), $product->getTitle(), $defaultCategory->getTitle(), $cartItem->getRealPrice(), $cartItem->getQuantity()); } $this->tracker->doTrackEcommerceCartUpdate($cart->getTotalAmount()); }
/** * Get the selected legacy product attributes. * * @param CartEvent $event */ protected function getLegacyProductAttributes(CartEvent $event) { $product = ProductQuery::create()->findPk($event->getProduct()); $productAttributes = $product->getTemplate()->getAttributes(); $this->legacyProductAttributes = []; /** @var Attribute $productAttribute */ foreach ($productAttributes as $productAttribute) { $legacyProductAttributeFieldKey = CartAddFormExtension::LEGACY_PRODUCT_ATTRIBUTE_FIELD_PREFIX . $productAttribute->getId(); if ($event->{$legacyProductAttributeFieldKey} !== null) { $this->legacyProductAttributes[$productAttribute->getId()] = $event->{$legacyProductAttributeFieldKey}; } } }
/** * Set the good item's price when added to cart * * @param CartEvent $event * @throws \Exception * @throws \Propel\Runtime\Exception\PropelException */ public function itemAddedToCart(CartEvent $event) { $cartItem = $event->getCartItem(); // Check if the product has some digressive prices $dpq = DigressivePriceQuery::create()->findByProductId($cartItem->getProductId()); if (count($dpq) != 0) { // Check if the quantity is into a range $dpq = DigressivePriceQuery::create()->filterByProductId($cartItem->getProductId())->filterByQuantityFrom($cartItem->getQuantity(), Criteria::LESS_EQUAL)->filterByQuantityTo($cartItem->getQuantity(), Criteria::GREATER_EQUAL)->find(); if ($dpq->count() === 1) { // Change cart item's prices with those from the corresponding range $cartItem->setPrice($dpq[0]->getPrice())->setPromoPrice($dpq[0]->getPromoPrice())->save(); } else { // Change cart item's prices with the default out of range ones $prices = ProductPriceQuery::create()->findOneByProductSaleElementsId($cartItem->getProductSaleElementsId()); $cartItem->setPrice($prices->getPrice())->setPromoPrice($prices->getPromoPrice())->save(); } } }
/** * Find a specific record in CartItem table using the current CartEvent * * @param CartEvent $event the cart event */ public function findCartItem(CartEvent $event) { if (null !== ($foundItem = CartItemQuery::create()->filterByCartId($event->getCart()->getId())->filterByProductId($event->getProduct())->filterByProductSaleElementsId($event->getProductSaleElementsId())->findOne())) { $event->setCartItem($foundItem); } }
public function tntCalculCartWeight(CartEvent $event) { $event->getCart()->setVirtualColumn('total_weight', $event->getCart()->getWeight()); $maxWeightPackage = TNTFrance::getConfigValue(TNTFranceConfigValue::MAX_WEIGHT_PACKAGE, 25); $totalPackage = 0; //If packages are separated per product if (1 == TNTFrance::getConfigValue(TNTFranceConfigValue::SEPARATE_PRODUCT_IN_PACKAGE, 0)) { /** @var \Thelia\Model\CartItem $cartItem */ foreach ($event->getCart()->getCartItems() as $cartItem) { if (null != ($pse = ProductSaleElementsQuery::create()->findPk($cartItem->getProductSaleElementsId()))) { $totalPackage += ceil($cartItem->getQuantity() * $pse->getWeight() / $maxWeightPackage); } } } else { $totalPackage += ceil($event->getCart()->getVirtualColumn('total_weight') / $maxWeightPackage); } $event->getCart()->setVirtualColumn('total_package', $totalPackage); }
/** * Find a specific record in CartItem table using the current CartEvent * * @param CartEvent $event the cart event */ public function findCartItem(CartEvent $event) { // Do not try to find a cartItem if one exists in the event, as previous event handlers // mays have put it in th event. if (null === $event->getCartItem() && null !== ($foundItem = CartItemQuery::create()->filterByCartId($event->getCart()->getId())->filterByProductId($event->getProduct())->filterByProductSaleElementsId($event->getProductSaleElementsId())->findOne())) { $event->setCartItem($foundItem); } }
/** * * Modify article's quantity * * don't use Form here just test the Request. * * @param \Thelia\Core\Event\Cart\CartEvent $event */ public function changeItem(CartEvent $event) { if (null !== ($cartItemId = $event->getCartItem()) && null !== ($quantity = $event->getQuantity())) { $cart = $event->getCart(); $cartItem = CartItemQuery::create()->filterByCartId($cart->getId())->filterById($cartItemId)->findOne(); if ($cartItem) { $event->setCartItem($this->updateQuantity($event->getDispatcher(), $cartItem, $quantity)); } } }
/** * @inheritdoc */ public function exec() { $discount = 0; $cartItems = $this->facade->getCart()->getCartItems(); /** @var Product $eligibleProduct */ $eligibleProduct = null; /** @var CartItem $cartItem */ foreach ($cartItems as $cartItem) { if (in_array($cartItem->getProduct()->getId(), $this->product_list)) { if (!$cartItem->getPromo() || $this->isAvailableOnSpecialOffers()) { $eligibleProduct = $cartItem; break; } } } if ($eligibleProduct !== null) { // Get the cart item for the eligible product $freeProductCartItem = $this->getRelatedCartItem($eligibleProduct); // We add the free product it only if it not yet in the cart. if ($freeProductCartItem === false) { if (null !== ($freeProduct = ProductQuery::create()->findPk($this->offeredProductId))) { // Store in the session that the free product is added to the cart, // so that we don't enter the following infinite loop : // // 1) exec() adds a product by firing a CART_ADDITEM event, // 2) the event is processed by Action\Coupon::updateOrderDiscount(), // 3) Action\Coupon::updateOrderDiscount() calls CouponManager::getDiscount() // 4) CouponManager::getDiscount() calls exec() -> Infinite loop !! // Store a marker first, we do not have the cart item id yet. $this->setRelatedCartItem($eligibleProduct, self::ADD_TO_CART_IN_PROCESS); $cartEvent = new CartEvent($this->facade->getCart()); $cartEvent->setNewness(true); $cartEvent->setAppend(false); $cartEvent->setQuantity(1); $cartEvent->setProductSaleElementsId($freeProduct->getDefaultSaleElements()->getId()); $cartEvent->setProduct($this->offeredProductId); $this->facade->getDispatcher()->dispatch(TheliaEvents::CART_ADDITEM, $cartEvent); // Store the final cart item ID. $this->setRelatedCartItem($eligibleProduct, $cartEvent->getCartItem()->getId()); $freeProductCartItem = $cartEvent->getCartItem(); } } if ($freeProductCartItem instanceof CartItem) { // The discount is the product price. $discount = $freeProductCartItem->getPromo() ? $freeProductCartItem->getPromoPrice() : $freeProductCartItem->getPrice(); } } else { // Remove all free products for this coupon, but no not remove the product from the cart. $this->clearFreeProductsCartItemIds(); } return $discount; }
/** * @return Cart */ public function getDuplicatedCart() { return parent::getCart(); }