diff --git a/core/lib/Thelia/Action/Coupon.php b/core/lib/Thelia/Action/Coupon.php index b476af6b4..e3a8defbf 100755 --- a/core/lib/Thelia/Action/Coupon.php +++ b/core/lib/Thelia/Action/Coupon.php @@ -35,6 +35,7 @@ use Thelia\Coupon\CouponManager; use Thelia\Coupon\ConditionCollection; use Thelia\Coupon\Type\CouponInterface; use Thelia\Model\Coupon as CouponModel; +use Thelia\Model\CouponQuery; /** * Created by JetBrains PhpStorm. @@ -120,8 +121,12 @@ class Coupon extends BaseAction implements EventSubscriberInterface $request->getSession()->setConsumedCoupons($consumedCoupons); $totalDiscount = $couponManager->getDiscount(); + // @todo insert false product in cart with the name of the coupon and the discount as negative price - // @todo decrement coupon quantity + // Decrement coupon quantity + $couponQuery = CouponQuery::create(); + $couponModel = $couponQuery->findOneByCode($coupon->getCode()); + $couponManager->decrementeQuantity($couponModel); $request->getSession()->getCart()->setDiscount($totalDiscount); diff --git a/core/lib/Thelia/Controller/Admin/CouponController.php b/core/lib/Thelia/Controller/Admin/CouponController.php index 63e9abaef..59ef312d5 100755 --- a/core/lib/Thelia/Controller/Admin/CouponController.php +++ b/core/lib/Thelia/Controller/Admin/CouponController.php @@ -28,9 +28,6 @@ use Symfony\Component\Routing\Router; use Thelia\Condition\ConditionFactory; use Thelia\Condition\ConditionManagerInterface; use Thelia\Core\Security\Resource\AdminResources; -use Thelia\Core\Event\Condition\ConditionCreateOrUpdateEvent; -use Thelia\Core\Event\AdminResources; -use Thelia\Core\Event\Coupon\CouponConsumeEvent; use Thelia\Core\Event\Coupon\CouponCreateOrUpdateEvent; use Thelia\Core\Event\TheliaEvents; use Thelia\Core\Security\AccessManager; diff --git a/core/lib/Thelia/Coupon/CouponManager.php b/core/lib/Thelia/Coupon/CouponManager.php index 0b4a9e9e8..1b446f1e0 100644 --- a/core/lib/Thelia/Coupon/CouponManager.php +++ b/core/lib/Thelia/Coupon/CouponManager.php @@ -26,6 +26,7 @@ namespace Thelia\Coupon; use Symfony\Component\DependencyInjection\ContainerInterface; use Thelia\Condition\ConditionManagerInterface; use Thelia\Coupon\Type\CouponInterface; +use Thelia\Model\Coupon; /** * Created by JetBrains PhpStorm. @@ -228,4 +229,34 @@ class CouponManager { return $this->availableConditions; } + + /** + * Decrement this coupon quantity + * + * To call when a coupon is consumed + * + * @param \Thelia\Model\Coupon $coupon Coupon consumed + * + * @return bool + */ + public function decrementeQuantity(Coupon $coupon) + { + $ret = true; + try { + + $oldMaxUsage = $coupon->getMaxUsage(); + + if ($oldMaxUsage > 0) { + $oldMaxUsage--; + $coupon->setMaxUsage($$oldMaxUsage); + + $coupon->save(); + } + + } catch(\Exception $e) { + $ret = false; + } + + return $ret; + } } \ No newline at end of file diff --git a/core/lib/Thelia/Coupon/Type/CouponAbstract.php b/core/lib/Thelia/Coupon/Type/CouponAbstract.php index 35ce3db4e..d3107faa7 100644 --- a/core/lib/Thelia/Coupon/Type/CouponAbstract.php +++ b/core/lib/Thelia/Coupon/Type/CouponAbstract.php @@ -307,5 +307,4 @@ abstract class CouponAbstract implements CouponInterface return $this->conditionEvaluator->isMatching($this->conditions); } - }