/**
  * @return static
  */
 public function withCoupons()
 {
     foreach ($this->entity->getCoupons() as $key => $coupon) {
         $this->entityDTO->coupons[$key] = $this->dtoBuilderFactory->getCouponDTOBuilder($coupon)->build();
     }
     return $this;
 }
 private function calculateCouponDiscounts()
 {
     foreach ($this->cart->getCoupons() as $key => $coupon) {
         if ($coupon->isValid($this->pricing->getDate(), $this->cartTotal->subtotal)) {
             $newSubtotal = $coupon->getUnitPrice($this->cartTotal->subtotal);
             $discountValue = $this->cartTotal->subtotal - $newSubtotal;
             $this->cartTotal->discount += $discountValue;
             $this->cartTotal->coupons[$key] = $coupon;
             if ($coupon->getReducesTaxSubtotal()) {
                 $this->cartTotal->taxSubtotal -= $discountValue;
             }
             if ($coupon->getFlagFreeShipping()) {
                 $this->cartTotal->shippingDiscount = $this->cartTotal->shipping;
                 $this->cartTotal->discount += $this->cartTotal->shipping;
             }
         }
     }
     // No taxes below zero!
     $this->cartTotal->taxSubtotal = max(0, $this->cartTotal->taxSubtotal);
 }
Exemple #3
0
 /**
  * @param UuidInterface $orderId
  * @param User $user
  * @param Cart $cart
  * @param CartCalculatorInterface $cartCalculator
  * @param string $ip4
  * @return Order
  */
 public static function fromCart(UuidInterface $orderId, User $user, Cart $cart, CartCalculatorInterface $cartCalculator, $ip4)
 {
     $order = new Order($orderId);
     $order->setIp4($ip4);
     foreach ($cart->getCartItems() as $item) {
         $order->addOrderItem($item->getOrderItem($order, $cartCalculator->getPricing()));
     }
     foreach ($cart->getCoupons() as $coupon) {
         $order->addCoupon($coupon);
     }
     $order->setUser($user);
     $order->setTaxRate($cart->getTaxRate());
     $order->setShipmentRate($cart->getShipmentRate());
     $order->setTotal($cart->getTotal($cartCalculator));
     return $order;
 }
Exemple #4
0
 public function testRemoveCoupon()
 {
     $cart = new Cart();
     $coupon = $this->dummyData->getCoupon();
     $cart->addCoupon($coupon);
     $cart->removeCoupon($coupon);
     $this->assertCount(0, $cart->getCoupons());
 }