/** * 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) { // 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)); } } }