Exemple #1
0
 /**
  *
  * add an article in the current cart
  * @param \Thelia\Core\Event\Cart\CartEvent $event
  */
 public function addItem(CartEvent $event)
 {
     $cart = $event->getCart();
     $newness = $event->getNewness();
     $append = $event->getAppend();
     $quantity = $event->getQuantity();
     $currency = $cart->getCurrency();
     $customer = $cart->getCustomer();
     $discount = 0;
     if ($cart->isNew()) {
         $persistEvent = new CartPersistEvent($cart);
         $event->getDispatcher()->dispatch(TheliaEvents::CART_PERSIST, $persistEvent);
     }
     if (null !== $customer && $customer->getDiscount() > 0) {
         $discount = $customer->getDiscount();
     }
     $productSaleElementsId = $event->getProductSaleElementsId();
     $productId = $event->getProduct();
     // Search for an identical item in the cart
     $event->getDispatcher()->dispatch(TheliaEvents::CART_FINDITEM, $event);
     $cartItem = $event->getCartItem();
     if ($cartItem === null || $newness) {
         $productSaleElements = ProductSaleElementsQuery::create()->findPk($productSaleElementsId);
         if (null !== $productSaleElements) {
             $productPrices = $productSaleElements->getPricesByCurrency($currency, $discount);
             $cartItem = $this->doAddItem($event->getDispatcher(), $cart, $productId, $productSaleElements, $quantity, $productPrices);
         }
     } elseif ($append && $cartItem !== null) {
         $cartItem->addQuantity($quantity)->save();
     }
     $event->setCartItem($cartItem);
 }
Exemple #2
0
 /**
  *
  * add an article in the current cart
  * @param \Thelia\Core\Event\Cart\CartEvent $event
  */
 public function addItem(CartEvent $event)
 {
     $cart = $event->getCart();
     $newness = $event->getNewness();
     $append = $event->getAppend();
     $quantity = $event->getQuantity();
     $currency = $cart->getCurrency();
     $customer = $cart->getCustomer();
     $discount = 0;
     if (null !== $customer && $customer->getDiscount() > 0) {
         $discount = $customer->getDiscount();
     }
     $productSaleElementsId = $event->getProductSaleElementsId();
     $productId = $event->getProduct();
     $cartItem = $this->findItem($cart->getId(), $productId, $productSaleElementsId);
     if ($cartItem === null || $newness) {
         $productSaleElements = ProductSaleElementsQuery::create()->findPk($productSaleElementsId);
         if (null !== $productSaleElements) {
             $productPrices = $productSaleElements->getPricesByCurrency($currency, $discount);
             $event->setCartItem($this->doAddItem($event->getDispatcher(), $cart, $productId, $productSaleElements, $quantity, $productPrices));
         }
     }
     if ($append && $cartItem !== null) {
         $cartItem->addQuantity($quantity)->save();
         $event->setCartItem($cartItem);
     }
 }