/** * 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; }
/** * 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); } }
/** * Creates a new coupon instance, given an existing Coupon as reference. * * You can specify a DateTime new coupon will be valid from. * If not specified, current DateTime will be used * * If given coupon is valid forever, new coupon will also be * Otherwise, this method will add to validFrom, the same interval than given Coupon * * Also can be specified how new Coupon name must be defined. * If none, automatic generator will add to existing name, 10 random digits. * * Given Coupon name: FOO * New Coupon name: FOO_a67b6786a6 * * Coupons are only generated, and are not persisted in Manager nor Flushed * * @param CouponInterface $coupon Reference coupon * @param DateTime $dateFrom Date From. If null, takes actual dateTime * * @return CouponInterface Coupon generated */ public function duplicateCoupon(CouponInterface $coupon, DateTime $dateFrom = null) { /** * Creates a valid date interval given the referent Coupon. */ if (null === $dateFrom) { $dateFrom = $this->dateTimeFactory->create(); } $dateTo = null; if ($coupon->getValidTo() instanceof DateTime) { $interval = $coupon->getValidFrom()->diff($coupon->getValidTo()); $dateTo = clone $dateFrom; $dateTo->add($interval); } /** * @var CouponInterface $couponGenerated */ $couponGenerated = $this->couponFactory->create(); $couponCode = $this->couponCodeGenerator->generate(10); $couponGenerated->setCode($couponCode)->setName($coupon->getName())->setType($coupon->getType())->setPrice($coupon->getPrice())->setDiscount($coupon->getDiscount())->setCount($coupon->getCount())->setPriority($coupon->getPriority())->setMinimumPurchase($coupon->getMinimumPurchase())->setValidFrom($dateFrom)->setValidTo($dateTo)->setRule($coupon->getRule())->setEnforcement($coupon->getEnforcement())->setEnabled(true); return $couponGenerated; }
/** * 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); }