From bc57c879c0659d2aa55f273e89242b0bd191abf3 Mon Sep 17 00:00:00 2001 From: Franck Allimant Date: Tue, 22 Jul 2014 18:59:19 +0200 Subject: [PATCH] Added in_use parameter to the loop --- core/lib/Thelia/Core/Template/Loop/Coupon.php | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/core/lib/Thelia/Core/Template/Loop/Coupon.php b/core/lib/Thelia/Core/Template/Loop/Coupon.php index d1d26c905..b313250a9 100644 --- a/core/lib/Thelia/Core/Template/Loop/Coupon.php +++ b/core/lib/Thelia/Core/Template/Loop/Coupon.php @@ -49,6 +49,7 @@ class Coupon extends BaseI18nLoop implements PropelSearchLoopInterface return new ArgumentCollection( Argument::createIntListTypeArgument('id'), Argument::createBooleanOrBothTypeArgument('is_enabled'), + Argument::createBooleanTypeArgument('in_use'), new Argument( 'order', new TypeCollection( @@ -86,6 +87,18 @@ class Coupon extends BaseI18nLoop implements PropelSearchLoopInterface $search->filterByIsEnabled($isEnabled ? true : false); } + $inUse = $this->getInUse(); + + if ($inUse !== null) { + // Get the code of coupons currently in use + $consumedCoupons = $this->request->getSession()->getConsumedCoupons(); + + // Get only matching coupons. + $criteria = $inUse ? Criteria::IN : Criteria::NOT_IN; + + $search->filterByCode($consumedCoupons, $criteria); + } + $search->addAsColumn('days_left', 'DATEDIFF('.CouponTableMap::EXPIRATION_DATE.', CURDATE()) - 1'); $orders = $this->getOrder(); @@ -151,6 +164,9 @@ class Coupon extends BaseI18nLoop implements PropelSearchLoopInterface /** @var ConditionFactory $conditionFactory */ $conditionFactory = $this->container->get('thelia.condition.factory'); + /** @var array $consumedCoupons an array of coupon code currently in use */ + $consumedCoupons = $this->request->getSession()->getConsumedCoupons(); + /** @var MCoupon $coupon */ foreach ($loopResult->getResultDataCollection() as $coupon) { @@ -202,6 +218,12 @@ class Coupon extends BaseI18nLoop implements PropelSearchLoopInterface $freeShippingForModulesIds[] = $couponModule->getModuleId(); } + // If and only if the coupon is currently in use, get the coupon discount. Calling exec() on a coupon + // which is not currently in use may apply coupon on the cart. This is true for coupons such as FreeProduct, + // which adds a product to the cart. + $discount = $couponManager->isInUse() ? $couponManager->exec() : 0; + + $loopResultRow ->set("ID", $coupon->getId()) ->set("IS_TRANSLATED", $coupon->getVirtualColumn('IS_TRANSLATED')) @@ -224,6 +246,7 @@ class Coupon extends BaseI18nLoop implements PropelSearchLoopInterface ->set("SERVICE_ID", $couponManager->getServiceId()) ->set("FREE_SHIPPING_FOR_COUNTRIES_LIST", implode(',', $freeShippingForCountriesIds)) ->set("FREE_SHIPPING_FOR_MODULES_LIST", implode(',', $freeShippingForModulesIds)) + ->set("DISCOUNT_AMOUNT", $discount) ; $loopResult->addRow($loopResultRow);