/**
  * Calculate coupon absolute value.
  *
  * @param CartInterface   $cart   Cart
  * @param CouponInterface $coupon Coupon
  *
  * @return MoneyInterface Absolute value for this coupon in this cart
  */
 public function getCouponAbsoluteValue(CartInterface $cart, CouponInterface $coupon)
 {
     $currency = $this->currencyWrapper->get();
     $couponPrice = Money::create(0, $currency);
     foreach ($this->cartCouponApplicators as $cartCouponApplicator) {
         if ($cartCouponApplicator->canBeApplied($cart, $coupon)) {
             return $cartCouponApplicator->getCouponAbsoluteValue($cart, $coupon);
         }
     }
     return $couponPrice;
 }
 /**
  * Calculates coupons price given actual Cart.
  *
  * @param CartInterface $cart Cart
  */
 public function refreshCouponAmount(CartInterface $cart)
 {
     $couponAmount = Money::create(0, $this->currencyWrapper->get());
     $coupons = $this->cartCouponManager->getCoupons($cart);
     foreach ($coupons as $coupon) {
         $currentCouponAmount = $this->cartCouponApplicatorCollector->getCouponAbsoluteValue($cart, $coupon);
         $coupon->setAbsolutePrice($currentCouponAmount);
         $couponAmount = $couponAmount->add($currentCouponAmount);
     }
     $cart->setCouponAmount($couponAmount);
 }
 /**
  * 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;
 }
Example #4
0
 /**
  * Return a formatted price given the price in an integer format.
  *
  * Takes the currency from CurrencyWrapper
  *
  * @param int $value Value
  *
  * @return string The formatted price
  */
 public function printMoneyFromValue($value)
 {
     $targetCurrency = $this->currencyWrapper->get();
     $money = Money::create($value, $targetCurrency);
     return $this->printMoney($money);
 }
 /**
  * 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)
 {
     $currency = $this->currencyWrapper->get();
     $amount = $coupon->getPrice();
     return $this->currencyConverter->convertMoney($amount, $currency);
 }