From b74d5d9a344d8035c570f1c7fdf53bef2db677cc Mon Sep 17 00:00:00 2001 From: Franck Allimant Date: Tue, 22 Jul 2014 16:31:16 +0200 Subject: [PATCH 1/6] Added ID in language list. --- templates/backOffice/default/languages.html | 2 ++ 1 file changed, 2 insertions(+) diff --git a/templates/backOffice/default/languages.html b/templates/backOffice/default/languages.html index 9678fbfe4..d5a3ec367 100644 --- a/templates/backOffice/default/languages.html +++ b/templates/backOffice/default/languages.html @@ -48,6 +48,7 @@ + {intl l="ID"} {intl l="Language name"} {intl l="ISO 639 Code"} {intl l="Locale"} @@ -58,6 +59,7 @@ {loop type="lang" name="lang.list" backend_context="1"} + {$ID} {$CODE} {$TITLE} {$CODE} From 6273179986076f059a1f352e0e5a6f308fccf55e Mon Sep 17 00:00:00 2001 From: Franck Allimant Date: Tue, 22 Jul 2014 18:58:55 +0200 Subject: [PATCH 2/6] Added a default value for getConsumedCoupons() --- core/lib/Thelia/Core/HttpFoundation/Session/Session.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/lib/Thelia/Core/HttpFoundation/Session/Session.php b/core/lib/Thelia/Core/HttpFoundation/Session/Session.php index 008f0fbfb..dbfe0d204 100644 --- a/core/lib/Thelia/Core/HttpFoundation/Session/Session.php +++ b/core/lib/Thelia/Core/HttpFoundation/Session/Session.php @@ -270,6 +270,6 @@ class Session extends BaseSession */ public function getConsumedCoupons() { - return $this->get('thelia.consumed_coupons'); + return $this->get('thelia.consumed_coupons', array()); } } From bc57c879c0659d2aa55f273e89242b0bd191abf3 Mon Sep 17 00:00:00 2001 From: Franck Allimant Date: Tue, 22 Jul 2014 18:59:19 +0200 Subject: [PATCH 3/6] 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); From bfd9a6ac7d6832f4726b6e4dfc5cea1f84d92a34 Mon Sep 17 00:00:00 2001 From: Franck Allimant Date: Tue, 22 Jul 2014 18:59:58 +0200 Subject: [PATCH 4/6] Added isInUse() method to the CouponInterface --- core/lib/Thelia/Coupon/Type/CouponAbstract.php | 18 +++++++++++++----- .../lib/Thelia/Coupon/Type/CouponInterface.php | 18 +++++++++++------- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/core/lib/Thelia/Coupon/Type/CouponAbstract.php b/core/lib/Thelia/Coupon/Type/CouponAbstract.php index 9766b7fbe..a0bdf2db1 100644 --- a/core/lib/Thelia/Coupon/Type/CouponAbstract.php +++ b/core/lib/Thelia/Coupon/Type/CouponAbstract.php @@ -183,6 +183,7 @@ abstract class CouponAbstract implements CouponInterface /** * @param true $perCustomerUsageCount + * @return $this */ public function setPerCustomerUsageCount($perCustomerUsageCount) { @@ -425,14 +426,14 @@ abstract class CouponAbstract implements CouponInterface * This methods checks a field value. If the field has a correct value, this value is returned * Otherwise, an InvalidArgumentException describing the problem should be thrown. * - * This method should be ovveriden to be useful. + * This method should be overriden to be useful. * - * @param $fieldName - * @param $fieldValue + * @param string $fieldName + * @param string $fieldValue * @return mixed - * @throws \InvalidArgumentException if the field valiue is not valid. + * @throws \InvalidArgumentException if the field value is not valid. */ - protected function checkCouponFieldValue($fieldName, $fieldValue) + protected function checkCouponFieldValue(/** @noinspection PhpUnusedParameterInspection */ $fieldName, $fieldValue) { return $fieldValue; } @@ -507,4 +508,11 @@ abstract class CouponAbstract implements CouponInterface { // Does nothing. Override this function as needed. } + + public function isInUse() { + return in_array( + $this->code, + $this->facade->getRequest()->getSession()->getConsumedCoupons() + ); + } } diff --git a/core/lib/Thelia/Coupon/Type/CouponInterface.php b/core/lib/Thelia/Coupon/Type/CouponInterface.php index d8190f3d7..cfd645d28 100644 --- a/core/lib/Thelia/Coupon/Type/CouponInterface.php +++ b/core/lib/Thelia/Coupon/Type/CouponInterface.php @@ -192,15 +192,15 @@ interface CouponInterface public function isExpired(); /** - * Return effects generated by the coupon - * A positive value + * Return an amount thant will be subtracted to the cart total, or zero. * - * Effects could also affect something else than the final Checkout price - * FacadeInterface $facade could be used to directly pass a Session value - * some would wish to modify - * Hence affecting a wide variety of Thelia elements + * This method could also perform something else than the calculating an amount to subtract from the cart. It may + * add a product to the cart, for example. In this case, an amount of 0 will be returned. * - * @return float Amount removed from the Total Checkout + * WARNING: this method could be called several times, so perform suitable checks before performing cart + * manipulations, so that the coupon effect will not be applied several times. + * + * @return float Amount removed from the cart total */ public function exec(); @@ -244,4 +244,8 @@ interface CouponInterface */ public function clear(); + /** + * @return bool true if the coupon is currently in use in the current order process, false otherwise + */ + public function isInUse(); } From 94257eeb8692f9f29cc87680645fc94d1f4632a8 Mon Sep 17 00:00:00 2001 From: Franck Allimant Date: Tue, 22 Jul 2014 19:01:18 +0200 Subject: [PATCH 5/6] The request object is \Thelia\Core\HttpFoundation\Request --- core/lib/Thelia/Core/Template/Element/BaseLoop.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/lib/Thelia/Core/Template/Element/BaseLoop.php b/core/lib/Thelia/Core/Template/Element/BaseLoop.php index 1038541de..bb88f5468 100644 --- a/core/lib/Thelia/Core/Template/Element/BaseLoop.php +++ b/core/lib/Thelia/Core/Template/Element/BaseLoop.php @@ -33,7 +33,7 @@ use Thelia\Type\TypeCollection; abstract class BaseLoop { /** - * @var \Symfony\Component\HttpFoundation\Request + * @var \Thelia\Core\HttpFoundation\Request */ protected $request; From 226818078667ddbdaa91dd4b3003e126344f6d8d Mon Sep 17 00:00:00 2001 From: Franck Allimant Date: Tue, 22 Jul 2014 19:43:39 +0200 Subject: [PATCH 6/6] Removed unused variable --- core/lib/Thelia/Core/Template/Loop/Coupon.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/core/lib/Thelia/Core/Template/Loop/Coupon.php b/core/lib/Thelia/Core/Template/Loop/Coupon.php index b313250a9..145202120 100644 --- a/core/lib/Thelia/Core/Template/Loop/Coupon.php +++ b/core/lib/Thelia/Core/Template/Loop/Coupon.php @@ -164,9 +164,6 @@ 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) {