Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Observer sales_order_paymentCancel and integrity constraint violation #1220

Closed
luigifab opened this issue Sep 21, 2020 · 0 comments · Fixed by #1473
Closed

Observer sales_order_paymentCancel and integrity constraint violation #1220

luigifab opened this issue Sep 21, 2020 · 0 comments · Fixed by #1473
Labels

Comments

@luigifab
Copy link
Contributor

luigifab commented Sep 21, 2020

In Mage_SalesRule_Model_Observer::sales_order_paymentCancel($observer) we have encoutered some Integrity constraint violation. Perhaps our database is little fucked, I have not investigated more, but I do the following to not raise an error.

diff --git a/web/app/code/core/Mage/SalesRule/Model/Observer.php b/web/app/code/core/Mage/SalesRule/Model/Observer.php
index aac4f9327..ad9bc2744 100644
--- a/web/app/code/core/Mage/SalesRule/Model/Observer.php
+++ b/web/app/code/core/Mage/SalesRule/Model/Observer.php
@@ -158,7 +158,8 @@ class Mage_SalesRule_Model_Observer
                     Mage::getResourceModel('salesrule/coupon_usage')->updateCustomerCouponTimesUsed($customerId, $coupon->getId(), true);
 
                     // Decrement rule times_used
-                    if ($customerCoupon = Mage::getModel('salesrule/rule_customer')->loadByCustomerRule($customerId, $coupon->getRuleId())) {
+                    $customerCoupon = Mage::getModel('salesrule/rule_customer')->loadByCustomerRule($customerId, $coupon->getRuleId());
+                    if (!empty($customerCoupon) && !empty($customerCoupon->getId())) {
                         $customerCoupon->setTimesUsed($customerCoupon->getTimesUsed() - 1);
                         $customerCoupon->save();
                     }

It seems that the customerCoupon was not found.

Preconditions

  1. OpenMage 20.0.3 / 20.0.5
  2. PHP 7.2.31 / 7.4.6
  3. A pending order with a coupon code.

Steps to reproduce

  1. Unknown.

Expected result

  1. No error.

Actual result

  1. The following fatal error:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row:
a foreign key constraint fails (`shop`.`salesrule_customer`, CONSTRAINT 
`FK_SALESRULE_CUSTOMER_RULE_ID_SALESRULE_RULE_ID` FOREIGN KEY (`rule_id`) 
REFERENCES `salesrule` (`rule_id`) ON DELETE CASCADE ON UPDATE CASCA),
query was: INSERT INTO `salesrule_customer` (`times_used`) VALUES (?)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant