/** * 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); }
/** * 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; }
/** * Return a formatted price given an Money object and the target currency * * If money is null, print empty string * * @param MoneyInterface $money the Money object to print * @param CurrencyInterface $targetCurrency Iso code of the target currency (optional) * * @throws \Exception if source-target exchange is missing * * @return string The formatted price */ public function printConvertMoney(MoneyInterface $money = null, CurrencyInterface $targetCurrency = null) { if (!$money instanceof MoneyInterface) { return ''; } if (!$targetCurrency instanceof CurrencyInterface) { $targetCurrency = $this->currencyWrapper->loadCurrency(); } $moneyConverted = $this->currencyConverter->convertMoney($money, $targetCurrency); return $this->printMoney($moneyConverted); }
/** * 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); } }
/** * 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; }
/** * Calculates all the amounts for a given a Cart * * @param CartInterface $cart Cart * * @return CartInterface Cart */ protected function loadCartPrices(CartInterface $cart) { $currency = $this->currencyWrapper->loadCurrency(); $productAmount = Money::create(0, $currency); /** * Calculate Amount and ProductAmount */ foreach ($cart->getCartLines() as $cartLine) { /** * @var CartLineInterface $cartLine */ $cartLine = $this->loadCartLinePrices($cartLine); /** * @var MoneyInterface $productAmount * @var MoneyInterface $totalAmount */ $convertedProductAmount = $this->currencyConverter->convertMoney($cartLine->getProductAmount(), $currency); $productAmount = $productAmount->add($convertedProductAmount); } $cart->setProductAmount($productAmount)->setAmount($productAmount); }
/** * 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); }
/** * Returns a zero-initialized Money object * to be assigned to product prices * * @return MoneyInterface */ protected function createZeroAmountMoney() { return Money::create(0, $this->currencyWrapper->getDefaultCurrency()); }
/** * 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); }