From 1b97c6fd5772717484586bc6bd031d850e7e1cb1 Mon Sep 17 00:00:00 2001 From: Dean Williams Date: Fri, 21 Aug 2020 01:45:27 -0400 Subject: [PATCH] Prevent duplicate entry when updating salesrule_coupon_usage (#1117) * remove $timesUsed > 0 check to prevent duplicate entry * Prevent $timesUsed from going less than 0 --- .../SalesRule/Model/Resource/Coupon/Usage.php | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/app/code/core/Mage/SalesRule/Model/Resource/Coupon/Usage.php b/app/code/core/Mage/SalesRule/Model/Resource/Coupon/Usage.php index 32663a2776e..546808939ea 100644 --- a/app/code/core/Mage/SalesRule/Model/Resource/Coupon/Usage.php +++ b/app/code/core/Mage/SalesRule/Model/Resource/Coupon/Usage.php @@ -61,17 +61,20 @@ public function updateCustomerCouponTimesUsed($customerId, $couponId, $decrement $timesUsed = $read->fetchOne($select, array(':coupon_id' => $couponId, ':customer_id' => $customerId)); - if ($timesUsed !== false && $timesUsed > 0) { - $this->_getWriteAdapter()->update( - $this->getMainTable(), - array( - 'times_used' => $timesUsed + ($decrement ? -1 : 1) - ), - array( - 'coupon_id = ?' => $couponId, - 'customer_id = ?' => $customerId, - ) - ); + if ($timesUsed !== false) { + $timesUsed += ($decrement ? -1 : 1); + if($timesUsed >= 0) { + $this->_getWriteAdapter()->update( + $this->getMainTable(), + array( + 'times_used' => $timesUsed + ), + array( + 'coupon_id = ?' => $couponId, + 'customer_id = ?' => $customerId, + ) + ); + } } else { $this->_getWriteAdapter()->insert( $this->getMainTable(),