/**
  * 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();
     $couponPrice = Money::create(0, $currency);
     $value = $coupon->getValue();
     preg_match($this->regexp(), $value, $match);
     $m = (int) $match[1];
     $n = (int) $match[2];
     $expressionValue = isset($match[3]) ? $match[3] : '';
     $modifiers = isset($match[4]) ? $match[4] : '';
     $freePerGroup = $m - $n;
     $freePerGroup = max($freePerGroup, 0);
     foreach ($cart->getCartLines() as $cartLine) {
         $moneys = [];
         $purchasable = $cartLine->getPurchasable();
         $expressionEvaluator = $this->getExpressionLanguageInstance();
         $expressionResult = (empty($expressionValue) || $expressionEvaluator->evaluate($expressionValue, ['purchasable' => $purchasable])) && $this->evaluatePurchasableType($purchasable, $modifiers);
         if (true === $expressionResult) {
             $partialElements = $cartLine->getQuantity();
             for ($i = 0; $i < $partialElements; ++$i) {
                 $partialPurchasable = $cartLine->getPurchasable();
                 $moneys[] = $partialPurchasable->getReducedPrice()->getAmount() > 0 ? $partialPurchasable->getReducedPrice() : $partialPurchasable->getPrice();
             }
             $groups = $partialElements / $m;
             if ($groups > 0) {
                 $nbMoneys = $groups * $freePerGroup;
                 $moneysToDiscount = array_slice($moneys, 0, $nbMoneys);
                 foreach ($moneysToDiscount as $moneyToDiscount) {
                     $couponPrice = $couponPrice->add($this->currencyConverter->convertMoney($moneyToDiscount, $currency));
                 }
             }
         }
     }
     return $couponPrice;
 }
Esempio n. 2
0
 /**
  * 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)->setValue($coupon->getValue())->setRule($coupon->getRule())->setEnforcement($coupon->getEnforcement())->setEnabled(true);
     return $couponGenerated;
 }
 /**
  * Can be applied.
  *
  * @param CartInterface   $cart   Cart
  * @param CouponInterface $coupon Coupon
  *
  * @return bool Can be applied
  */
 public function canBeApplied(CartInterface $cart, CouponInterface $coupon)
 {
     return $coupon->getType() === self::id() && 1 === preg_match($this->regexp(), $coupon->getValue());
 }