[Forwardport]Fixed bug, when exception occurred on order with coupons cancel, made by guest after creating of customer account. #20572
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Original Pull Request
#19423
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 (*)