/** * 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); }
/** * Method subscribed to CartLoad event. * * Calculates coupons price given actual Cart, and overrides Cart price * given new information. * * @param CartOnLoadEvent $cartOnLoadEvent Event */ public function onCartLoadCoupons(CartOnLoadEvent $cartOnLoadEvent) { $cart = $cartOnLoadEvent->getCart(); $couponAmount = Money::create(0, $this->currencyWrapper->loadCurrency()); $coupons = $this->cartCouponManager->getCoupons($cart); /** * @var CouponInterface $coupon */ foreach ($coupons as $coupon) { $currentCouponAmount = $this->getPriceCoupon($cart, $coupon); $coupon->setAbsolutePrice($currentCouponAmount); $couponAmount = $couponAmount->add($currentCouponAmount); } $cart->setCouponAmount($couponAmount); $cart->setAmount($cart->getAmount()->subtract($couponAmount)); }
/** * 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); }