-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
Fixed bug, when exception occurred on order with coupons cancel, made by guest after creating of customer account. #19423
Fixed bug, when exception occurred on order with coupons cancel, made by guest after creating of customer account. #19423
Conversation
… by guest after creating of customer account.
Hi @KravetsAndriy. Thank you for your contribution
For more details, please, review the Magento Contributor Assistant documentation |
Going to fix CI issues tomorrow |
@magento-engcom-team give me 2.3-develop instance |
Hi @KravetsAndriy. Thank you for your request. I'm working on Magento 2.3-develop instance for you |
Hi @KravetsAndriy, here is your Magento instance. |
@sivaschenko Hello. How can I fix this issue:
|
Hi @Winfle , please move all the code from |
@sivaschenko unfortunately, I moved it many times from |
@sivaschenko you can see, that it's reproducible now. |
Hi @sivaschenko, thank you for the review. |
Hi @Winfle, thank you for your contribution! |
… cancel, made by guest after creating of customer account. #19423
Hi @Winfle. Thank you for your contribution. Please, consider to port this solution to 2.3 release line. |
Good afternoon, |
Hi @ecommercegluk according to the comment #19423 (comment) the earliest version this pull request can be applicable to is 2.2.8 |
Description (*)
When customer do checkout as a guest using a valid coupon, he makes a new customer account.
During trying to cancel this order, admin does receive SQL exception:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (
mage.
salesrule_customer, ...
Issue related to changes made: #19230
Fixed Issues (if relevant)
Scenario
Actual problem exist in scenarious when sales rule, used by guest user (using coupon code), it doesn't trigger its coupon usage per customer update, because there is no customer.
After creating of new customer (using of AccountDelegation action), Magento AssignOrderToCustomerObserver using customer_save_after_data_object event tries to assign to this customer order, that has been made previously.
As a result, this Magento\SalesRule\Model\Rule\Customer wasn't created for newly created customer, but normally it should. Furthermore, it should increase
time_used
attribute for new customer.As a result, during trying to cancel this type of orders, sales rule plugin is trying to decrease amount of usages for given customer. Actually, method which is responsible for it is not good enough, so it allows to save empty data (empty instert), which provokes cascade adding by foreign keys. As result, admin cannot cancel this order and get SQL error.
Fixing
So, obviously we have to create this entity on customer registration, when Magento assigns order to related new customer.
As you can see, all data we need is set inside of AssignOrderToCustomerObserver and we cannot subscribe on same event, because only this observer (Sales) knows about order and customer, that its going to be assigned.
So, not to mixing contexts (Sales and SalesRule, because assigning of coupon and sales usages happens using Magento\SalesRule\Model\Coupon\UpdateCouponUsages), I decided to make few extending/improvements:
sales_order_customer_assign_after
to let another module knows about certain customer has been attached to order.salerule_customer
model and increase usagetime_used
value to 1.So, this PR not only fixes exception on canceling orders, that has been made by guest before registering new account. But also fixes bug, when sales rule usage has not been increased on the same scenario.
Manual testing scenarios (*)
Checkout as a guest using a valid coupon
After placing the order, register an account using the create account button on the order confirmation page.
Login to the admin
Make sure to get the order to a processing state.
Try to cancel the order
Order is canceled correctly
Sales rule usage for newly created customer is made and set as 1
Contribution checklist (*)