/** * Occurring when a Coupon condition is about to be consumed * * @param CouponConsumeEvent $event Event consuming Coupon */ public function consume(CouponConsumeEvent $event) { $totalDiscount = 0; $isValid = false; /** @var CouponInterface $coupon */ $coupon = $this->couponFactory->buildCouponFromCode($event->getCode()); if ($coupon) { $isValid = $coupon->isMatching(); if ($isValid) { $consumedCoupons = $this->request->getSession()->getConsumedCoupons(); if (!isset($consumedCoupons) || !$consumedCoupons) { $consumedCoupons = array(); } if (!isset($consumedCoupons[$event->getCode()])) { // Prevent accumulation of the same Coupon on a Checkout $consumedCoupons[$event->getCode()] = $event->getCode(); $this->request->getSession()->setConsumedCoupons($consumedCoupons); } $totalDiscount = $this->couponManager->getDiscount(); $this->request->getSession()->getCart()->setDiscount($totalDiscount)->save(); $this->request->getSession()->getOrder()->setDiscount($totalDiscount); } } $event->setIsValid($isValid); $event->setDiscount($totalDiscount); }
/** * Occurring when a Coupon condition is about to be consumed * * @param CouponConsumeEvent $event Event consuming Coupon * @param $eventName * @param EventDispatcherInterface $dispatcher */ public function consume(CouponConsumeEvent $event, $eventName, EventDispatcherInterface $dispatcher) { $totalDiscount = 0; $isValid = false; /** @var CouponInterface $coupon */ $coupon = $this->couponFactory->buildCouponFromCode($event->getCode()); if ($coupon) { $isValid = $coupon->isMatching(); if ($isValid) { $this->couponManager->pushCouponInSession($event->getCode()); $totalDiscount = $this->couponManager->getDiscount(); $this->getSession()->getSessionCart($dispatcher)->setDiscount($totalDiscount)->save(); $this->getSession()->getOrder()->setDiscount($totalDiscount); } } $event->setIsValid($isValid); $event->setDiscount($totalDiscount); }