/**
  * Check if cart meets minimum price requirements for a coupon.
  *
  * @param CartInterface   $cart   Cart
  * @param CouponInterface $coupon Coupon
  *
  * @throws CouponBelowMinimumPurchaseException Minimum value not reached
  */
 public function validateCartCouponMinimumPrice(CartInterface $cart, CouponInterface $coupon)
 {
     $couponMinimumPrice = $coupon->getMinimumPurchase();
     if ($couponMinimumPrice->getAmount() === 0) {
         return;
     }
     $productMoney = $cart->getProductAmount();
     if ($couponMinimumPrice->getCurrency() != $productMoney->getCurrency()) {
         $couponMinimumPrice = $this->currencyConverter->convertMoney($couponMinimumPrice, $productMoney->getCurrency());
     }
     if ($productMoney->isLessThan($couponMinimumPrice)) {
         throw new CouponBelowMinimumPurchaseException();
     }
 }
 /**
  * Given a cart and a coupon, returns the real value of the coupon.
  * If the type of the coupon is not valid, then an empty Money instance will
  * be returned, with value 0.
  *
  * @param CartInterface   $cart   Abstract Cart object
  * @param CouponInterface $coupon Coupon
  *
  * @return MoneyInterface Coupon price
  */
 public function getCouponAbsolutePrice(CartInterface $cart, CouponInterface $coupon)
 {
     $currency = $this->currencyWrapper->get();
     $couponPrice = Money::create(0, $currency);
     switch ($coupon->getType()) {
         case ElcodiCouponTypes::TYPE_PERCENT:
             $couponPercent = $coupon->getDiscount();
             $couponPrice = $cart->getProductAmount()->multiply($couponPercent / 100);
             break;
         case ElcodiCouponTypes::TYPE_AMOUNT:
             $amount = $coupon->getPrice();
             $couponPrice = $this->currencyConverter->convertMoney($amount, $currency);
             break;
     }
     return $couponPrice;
 }
示例#3
0
 /**
  * Load cart total price.
  *
  * @param CartInterface $cart Cart
  */
 public function loadCartTotalAmount(CartInterface $cart)
 {
     $currency = $this->currencyWrapper->get();
     $finalAmount = clone $cart->getProductAmount();
     /**
      * Calculates the shipping amount.
      */
     $shippingAmount = $cart->getShippingAmount();
     if ($shippingAmount instanceof MoneyInterface) {
         $convertedShippingAmount = $this->currencyConverter->convertMoney($shippingAmount, $currency);
         $finalAmount = $finalAmount->add($convertedShippingAmount);
     }
     /**
      * Calculates the coupon amount.
      */
     $couponAmount = $cart->getCouponAmount();
     if ($couponAmount instanceof MoneyInterface) {
         $convertedCouponAmount = $this->currencyConverter->convertMoney($couponAmount, $currency);
         $finalAmount = $finalAmount->subtract($convertedCouponAmount);
     }
     $cart->setAmount($finalAmount);
 }
示例#4
0
 /**
  * Given a cart and a coupon, returns the real value of the coupon
  *
  * @param CartInterface   $cart   Abstract Cart object
  * @param CouponInterface $coupon Coupon
  *
  * @return MoneyInterface Coupon price
  */
 protected function getPriceCoupon(CartInterface $cart, CouponInterface $coupon)
 {
     $currency = $this->currencyWrapper->getCurrency();
     switch ($coupon->getType()) {
         case ElcodiCouponTypes::TYPE_AMOUNT:
             $amount = $coupon->getPrice();
             return $this->currencyConverter->convertMoney($amount, $currency);
         case ElcodiCouponTypes::TYPE_PERCENT:
             $couponPercent = $coupon->getDiscount();
             return $cart->getProductAmount()->multiply($couponPercent / 100);
     }
 }
 /**
  * Calculate coupon absolute value.
  *
  * @param CartInterface   $cart   Cart
  * @param CouponInterface $coupon Coupon
  *
  * @return MoneyInterface|false Absolute value for this coupon in this cart.
  */
 public function getCouponAbsoluteValue(CartInterface $cart, CouponInterface $coupon)
 {
     $couponPercent = $coupon->getDiscount();
     return $cart->getProductAmount()->multiply($couponPercent / 100);
 }
 /**
  * Given ShippingPriceRange is satisfied by a cart
  *
  * @param CartInterface          $cart          Cart
  * @param ShippingRangeInterface $shippingRange Carrier Range
  *
  * @return boolean ShippingRange is satisfied by cart
  */
 private function isShippingPriceRangeSatisfiedByCart(CartInterface $cart, ShippingRangeInterface $shippingRange)
 {
     $cartPrice = $cart->getProductAmount();
     $cartPriceCurrency = $cartPrice->getCurrency();
     $shippingRangeFromPrice = $shippingRange->getFromPrice();
     $shippingRangeToPrice = $shippingRange->getToPrice();
     return $this->isShippingRangeZonesSatisfiedByCart($cart, $shippingRange) && $this->currencyConverter->convertMoney($shippingRangeFromPrice, $cartPriceCurrency)->compareTo($cartPrice) <= 0 && $this->currencyConverter->convertMoney($shippingRangeToPrice, $cartPriceCurrency)->compareTo($cartPrice) > 0;
 }