Skip to content

magento/magento2#19230: [Forwardport] Can't Cancel Order #20577

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

Merged
merged 6 commits into from
Aug 27, 2019

Conversation

p-bystritsky
Copy link
Contributor

Original PR #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)

  1. Can't Cancel Order #19230 : I Can't Cancel Order

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:

  1. Remove business logic from observer to related service, which will be responsible for delegating orders to customers.
  2. Fire up event event sales_order_customer_assign_after to let another module knows about certain customer has been attached to order.
  3. Catch in SalesRule module to create related salerule_customer model and increase usage time_used value to 1.
  4. Write functional testing, that represent all the flow.

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 (*)

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • All automated tests passed successfully (all builds on Travis CI are green)

@magento-engcom-team
Copy link
Contributor

Hi @p-bystritsky. Thank you for your contribution
Here is some useful tips how you can test your changes using Magento test environment.
Add the comment under your pull request to deploy test or vanilla Magento instance:

  • @magento-engcom-team give me test instance - deploy test instance based on PR changes
  • @magento-engcom-team give me 2.3-develop instance - deploy vanilla Magento instance

For more details, please, review the Magento Contributor Assistant documentation

@p-bystritsky
Copy link
Contributor Author

p-bystritsky commented Jan 24, 2019

Should be processed after merging #20579

@ghost
Copy link

ghost commented Jan 24, 2019

Hi @p-bystritsky, thank you for your contribution!
Please, complete Contribution Survey, it will take less than a minute.
Your feedback will help us to improve contribution process.

@ghost
Copy link

ghost commented Jul 22, 2019

@p-bystritsky unfortunately, only members of the maintainers team are allowed to remove progress related labels to the pull request

# Conflicts:
#	app/code/Magento/Sales/Observer/AssignOrderToCustomerObserver.php
#	app/code/Magento/Sales/Test/Unit/Observer/AssignOrderToCustomerObserverTest.php
@slavvka
Copy link
Member

slavvka commented Aug 8, 2019

@magento run all tests

@slavvka slavvka self-assigned this Aug 8, 2019
@magento-engcom-team
Copy link
Contributor

Hi @slavvka, thank you for the review.
ENGCOM-5475 has been created to process this Pull Request
✳️ @slavvka, could you please add one of the following labels to the Pull Request?

Label Description
Auto-Tests: Covered All changes in Pull Request is covered by auto-tests
Auto-Tests: Not Covered Changes in Pull Request requires coverage by auto-tests
Auto-Tests: Not Required Changes in Pull Request does not require coverage by auto-tests

@engcom-Alfa
Copy link
Contributor

✔️ QA Passed

Before:
before

After:
after

@m2-assistant
Copy link

m2-assistant bot commented Aug 27, 2019

Hi @p-bystritsky, thank you for your contribution!
Please, complete Contribution Survey, it will take less than a minute.
Your feedback will help us to improve contribution process.

@magento-engcom-team magento-engcom-team added this to the Release: 2.3.4 milestone Aug 27, 2019
@sidolov sidolov added the Auto-Tests: Covered All changes in Pull Request is covered by auto-tests label Sep 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants