Skip to content

Commit

Permalink
coupon usage limit eligibilityChecker fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Tetragramat committed Jun 15, 2015
1 parent 09e24fd commit 4175a6c
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
26 changes: 26 additions & 0 deletions src/Sylius/Bundle/CoreBundle/Doctrine/ORM/OrderRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,32 @@ public function countByUserAndCoupon(UserInterface $user, CouponInterface $coupo
return $count;
}

/**
* {@inheritdoc}
*/
public function countByCoupon(CouponInterface $coupon)
{
$this->_em->getFilters()->disable('softdeleteable');

$queryBuilder = $this->getQueryBuilder();
$queryBuilder
->select('count(o.id)')
->innerJoin('o.promotionCoupons', 'coupons')
->andWhere('o.completedAt IS NOT NULL')
->andWhere($queryBuilder->expr()->in('coupons', ':coupons'))
->setParameter('coupons', (array) $coupon)
;

$count = (int) $queryBuilder
->getQuery()
->getSingleScalarResult()
;

$this->_em->getFilters()->enable('softdeleteable');

return $count;
}

/**
* Create checkouts paginator.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ protected function areCouponsEligibleForPromotion(PromotionSubjectInterface $sub

if ($eligible) {
$this->dispatcher->dispatch(SyliusPromotionEvents::COUPON_ELIGIBLE, new GenericEvent($promotion));
} else {
$this->dispatcher->dispatch(SyliusPromotionEvents::COUPON_NOT_ELIGIBLE, new GenericEvent($promotion));
}

return $eligible;
Expand All @@ -86,6 +88,10 @@ private function isCouponEligibleToLimit(CouponInterface $coupon, PromotionInter
return true;
}

if (!$this->isCouponEligibleToCouponLimit($coupon)) {
return false;
}

if (!$coupon->getPerUserUsageLimit()) {
return true;
}
Expand All @@ -97,12 +103,26 @@ private function isCouponEligibleToLimit(CouponInterface $coupon, PromotionInter
return $this->isCouponEligible($coupon, $promotion, $user);
}

private function isCouponEligibleToCouponLimit(CouponInterface $coupon)
{
if (!$coupon->getUsageLimit()) {
return true;
}

$countPlacedOrders = $this->subjectRepository->countByCoupon($coupon);

if ($countPlacedOrders < $coupon->getUsageLimit()) {
return true;
}

return false;
}

private function isCouponEligible(CouponInterface $coupon, PromotionInterface $promotion, UserInterface $user)
{
$countPlacedOrders = $this->subjectRepository->countByUserAndCoupon($user, $coupon);

// <= because we need to include the cart orders as well
if ($countPlacedOrders <= $coupon->getPerUserUsageLimit()) {
if ($countPlacedOrders < $coupon->getPerUserUsageLimit()) {
$this->dispatcher->dispatch(SyliusPromotionEvents::COUPON_ELIGIBLE, new GenericEvent($promotion));

return true;
Expand Down
10 changes: 10 additions & 0 deletions src/Sylius/Component/Core/Repository/OrderRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ public function findExpired(\DateTime $expiresAt, $state = OrderInterface::STATE
*/
public function countByUserAndCoupon(UserInterface $user, CouponInterface $coupon);

/**
* Gets the number of orders placed
* for a particular coupon.
*
* @param CouponInterface $coupon
*
* @return int
*/
public function countByCoupon(CouponInterface $coupon);

/**
* Gets the number of orders placed by the user
* with particular state.
Expand Down

0 comments on commit 4175a6c

Please sign in to comment.