Fixed the checking of maximum coupon usage count
This commit is contained in:
@@ -69,7 +69,7 @@ class CouponFactory
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Check coupon usage count
|
// Check coupon usage count
|
||||||
if ($couponModel->getUsagesLeft($this->facade->getCustomer()->getId()) <= 0) {
|
if (! $couponModel->isUsageUnlimited() && $couponModel->getUsagesLeft($this->facade->getCustomer()->getId()) <= 0) {
|
||||||
throw new CouponNoUsageLeftException($couponCode);
|
throw new CouponNoUsageLeftException($couponCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -278,57 +278,62 @@ class CouponManager
|
|||||||
*/
|
*/
|
||||||
public function decrementQuantity(Coupon $coupon, $customerId = null)
|
public function decrementQuantity(Coupon $coupon, $customerId = null)
|
||||||
{
|
{
|
||||||
$ret = false;
|
if ($coupon->isUsageUnlimited()) {
|
||||||
|
$ret = true;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$ret = false;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
$usageLeft = $coupon->getUsagesLeft($customerId);
|
$usageLeft = $coupon->getUsagesLeft($customerId);
|
||||||
|
|
||||||
if ($usageLeft > 0) {
|
if ($usageLeft > 0) {
|
||||||
|
|
||||||
// If the coupon usage is per user, add an entry to coupon customer usage count table
|
// If the coupon usage is per user, add an entry to coupon customer usage count table
|
||||||
if ($coupon->getPerCustomerUsageCount()) {
|
if ($coupon->getPerCustomerUsageCount()) {
|
||||||
|
|
||||||
if (null == $customerId) {
|
if (null == $customerId) {
|
||||||
throw new \LogicException("Customer should not be null at this time.");
|
throw new \LogicException("Customer should not be null at this time.");
|
||||||
}
|
}
|
||||||
|
|
||||||
$ccc = CouponCustomerCountQuery::create()
|
$ccc = CouponCustomerCountQuery::create()
|
||||||
->filterByCouponId($coupon->getId())
|
->filterByCouponId($coupon->getId())
|
||||||
->filterByCustomerId($customerId)
|
->filterByCustomerId($customerId)
|
||||||
->findOne()
|
->findOne()
|
||||||
;
|
;
|
||||||
|
|
||||||
if ($ccc === null) {
|
if ($ccc === null) {
|
||||||
$ccc = new CouponCustomerCount();
|
$ccc = new CouponCustomerCount();
|
||||||
|
|
||||||
|
$ccc
|
||||||
|
->setCustomerId($customerId)
|
||||||
|
->setCouponId($coupon->getId())
|
||||||
|
->setCount(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
$newCount = 1 + $ccc->getCount();
|
||||||
|
|
||||||
$ccc
|
$ccc
|
||||||
->setCustomerId($customerId)
|
->setCount($newCount)
|
||||||
->setCouponId($coupon->getId())
|
->save()
|
||||||
->setCount(0);
|
;
|
||||||
|
|
||||||
|
$ret = $usageLeft - $newCount;
|
||||||
|
} else {
|
||||||
|
$usageLeft--;
|
||||||
|
|
||||||
|
$coupon->setMaxUsage($usageLeft);
|
||||||
|
|
||||||
|
$coupon->save();
|
||||||
|
|
||||||
|
$ret = $usageLeft;
|
||||||
}
|
}
|
||||||
|
|
||||||
$newCount = 1 + $ccc->getCount();
|
|
||||||
|
|
||||||
$ccc
|
|
||||||
->setCount($newCount)
|
|
||||||
->save()
|
|
||||||
;
|
|
||||||
|
|
||||||
$ret = $usageLeft - $newCount;
|
|
||||||
} else {
|
|
||||||
$usageLeft--;
|
|
||||||
|
|
||||||
$coupon->setMaxUsage($usageLeft);
|
|
||||||
|
|
||||||
$coupon->save();
|
|
||||||
|
|
||||||
$ret = $usageLeft;
|
|
||||||
}
|
}
|
||||||
|
} catch (\Exception $ex) {
|
||||||
|
// Just log the problem.
|
||||||
|
Tlog::getInstance()->addError(sprintf("Failed to decrement coupon %s: %s", $coupon->getCode(), $ex->getMessage()));
|
||||||
}
|
}
|
||||||
} catch (\Exception $ex) {
|
|
||||||
// Just log the problem.
|
|
||||||
Tlog::getInstance()->addError(sprintf("Failed to decrement coupon %s: %s", $coupon->getCode(), $ex->getMessage()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $ret;
|
return $ret;
|
||||||
|
|||||||
Reference in New Issue
Block a user