Working adding decrementation when coupon is consumed

This commit is contained in:
gmorel
2013-10-22 23:04:08 +02:00
parent b45fbef2ad
commit 52e471ab1b
4 changed files with 37 additions and 5 deletions

View File

@@ -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);

View File

@@ -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;

View File

@@ -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;
}
}

View File

@@ -307,5 +307,4 @@ abstract class CouponAbstract implements CouponInterface
return $this->conditionEvaluator->isMatching($this->conditions);
}
}