magento / magento2

Prior to making any Submission(s), you must sign an Adobe Contributor License Agreement, available here at: https://opensource.adobe.com/cla.html. All Submissions you make to Adobe Inc. and its affiliates, assigns and subsidiaries (collectively “Adobe”) are subject to the terms of the Adobe Contributor License Agreement.
http://www.magento.com
Open Software License 3.0
11.55k stars 9.32k forks source link

Quote is null in sales_quote_address_collect_totals_after #36908

Open xtremevision opened 1 year ago

xtremevision commented 1 year ago

Summary

Subscribing to events: sales_quote_address_collect_totals_after, sales_quote_collect_totals_before, sales_quote_collect_totals_after returns a null quote in the execute() function for the FIRST product added to the cart. Subsequent add product to cart call returns a quote entity.

I get the same result if I add a custom discount using sales.xml and section/group definition. Tested on Magento 2.4.4 and 2.4.5-p1, with php 8.1.

var/log/system.log output: [2023-02-23T02:29:48.772461+00:00] main.INFO: TestTotals1.php:execute - quote_id: [] [] [2023-02-23T02:29:48.786631+00:00] main.INFO: TotalsWithBonusProducts.php:collect - quote_id before ::collect() - [] [] [2023-02-23T02:29:48.788871+00:00] main.INFO: TestTotals3.php:execute - quote_id: [] [] [2023-02-23T02:29:48.944334+00:00] main.INFO: TotalsWithBonusProducts.php:collect - quote_id before ::collect() - [] [] [2023-02-23T02:29:48.944877+00:00] main.INFO: TestTotals3.php:execute - quote_id: [] [] [2023-02-23T02:29:48.967316+00:00] main.INFO: TestTotals2.php:execute - quote_id: [] [] (above is 1st product added to cart)

[2023-02-23T02:29:57.347658+00:00] main.INFO: TestTotals1.php:execute - quote_id: 21 [] [] [2023-02-23T02:29:57.357881+00:00] main.INFO: TotalsWithBonusProducts.php:collect - quote_id before ::collect() - 21 [] [] [2023-02-23T02:29:57.358358+00:00] main.INFO: TestTotals3.php:execute - quote_id: 21 [] [] [2023-02-23T02:29:57.475056+00:00] main.INFO: TotalsWithBonusProducts.php:collect - quote_id before ::collect() - 21 [] [] [2023-02-23T02:29:57.475349+00:00] main.INFO: TestTotals3.php:execute - quote_id: 21 [] [] [2023-02-23T02:29:57.490951+00:00] main.INFO: TestTotals2.php:execute - quote_id: 21 [] [] (above is 2nd product added to cart)

Observation: After the observers have been called, executed and the first product has been added to the cart, the quote record is created in the "quote" table.

Examples

Xtreme_Totals.zip

Proposed solution

As per official documentation, I would expect the quote record to be created before the observers are called, before any totals are collected, so the event will return a valid quote entity.

Scenario in which the flow is having an issue

Yes, I am having problems with the current flow, it is a critical issue for me. In particular adding my own discount fails because the first time the quote is null, so I can not store in the database anything referencing the quote_id.

I expressed myself wrong, I meant as per official core code, where these events are dispatched, I would expect the quote to be a valid entity.

example: image

My biggest concern is sales.xml which I use to add my own discount, and I must have a valid quote when it's called. Within the collect function I store information in my own table referencing the quote_id. If the quote_id is null I am unable to track the discount throughout the order life span, and beyond, after payment is made and our webhook is called with an IPN marking the order as paid, raising an invoice, which triggers additional external API calls - all looking up records based on quote_id (and order_id).

Let me know if you need more from me. Thank you for looking into this.

Release note

No response

Triage and priority

m2-assistant[bot] commented 1 year ago

Hi @xtremevision. Thank you for your report. To speed up processing of this issue, make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, Add a comment to the issue:

m2-assistant[bot] commented 1 year ago

Hi @engcom-Hotel. Thank you for working on this issue. In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:

engcom-Hotel commented 1 year ago

Hello @xtremevision,

Thanks for the report and collaboration!

We have tried to reproduce the issue in Magento 2.4-develop branch and the actual result is reproducible for us. Please refer to the below screenshot for reference:

image

It seems to us an expected behaviour, just a quick query, are you facing any issue with the existing flow and also can you please share the official documentation link related with this.

Thanks

xtremevision commented 1 year ago

Yes, I am having problems with the current flow, it is a critical issue for me. In particular adding my own discount fails because the first time the quote is null, so I can not store in the database anything referencing the quote_id.

I expressed myself wrong, I meant as per official core code, where these events are dispatched, I would expect the quote to be a valid entity.

example: image

My biggest concern is sales.xml which I use to add my own discount, and I must have a valid quote when it's called. Within the collect function I store information in my own table referencing the quote_id. If the quote_id is null I am unable to track the discount throughout the order life span, and beyond, after payment is made and our webhook is called with an IPN marking the order as paid, raising an invoice, which triggers additional external API calls - all looking up records based on quote_id (and order_id).

Let me know if you need more from me. Thank you for looking into this.

xtremevision commented 1 year ago

PS. From what I have seen, a lot of other people have experienced the same problem but have not necessarily noticed this specific pattern, i.e. the quote being null the 1st time, and valid the 2nd time.

xtremevision commented 1 year ago

I think I found the problem and fix. Please see below standard patch files. Not sure if this is the proper fix but it worked in my testing. Let me know if you want composer patches instead.

Thanks, Michael.

cart_model.patch quoterepository_savehandler.patch

engcom-Hotel commented 1 year ago

Hello @xtremevision,

Thanks for the updates!

It seems you have a valid scenario. We can move ahead to confirm this issue.

You can also create a PR if you already have a fix.

Thanks

github-jira-sync-bot commented 1 year ago

:white_check_mark: Jira issue https://jira.corp.adobe.com/browse/AC-8040 is successfully created for this GitHub issue.

m2-assistant[bot] commented 1 year ago

:white_check_mark: Confirmed by @engcom-Hotel. Thank you for verifying the issue.
Issue Available: @engcom-Hotel, You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself.

xtremevision commented 1 year ago

Hi,

I have opened a PR for this issue, and signed the CLA. Not sure what else needs to be done. Please assist.

Thanks, Michael.

SPbryan007 commented 1 year ago

@magento give me 2.4.5-p1 instance

magento-deployment-service[bot] commented 1 year ago

Hi @SPbryan007. Thank you for your request. I'm working on Magento instance for you.

magento-deployment-service[bot] commented 1 year ago

Hi @SPbryan007, here is your Magento Instance: https://f32c1a84a97840247905fa5d1edba0f0.instances.magento-community.engineering Admin access: https://f32c1a84a97840247905fa5d1edba0f0.instances.magento-community.engineering/admin_6c05 Login: 652d38fb Password: 40652b9e62fe

xtremevision commented 1 year ago

I've updated my patches. Will try them again with my PR as it's failing the unit testing.

xtremevision commented 1 year ago

Hi,

I managed to get the PR checked with 11 passed and 4 failing. The 4 failing though have nothing to do with me. Any ideas or suggestions?

Thanks.

engcom-Hotel commented 1 year ago

Hello @xtremevision,

If you feel like these failures are not due to your changes then try re-running the specific tests which are failing.

Thanks

xtremevision commented 1 year ago

I tried. They are still failing... :(

On 02/03/2023 11:10, Abhinav Pathak wrote:

Hello @xtremevision https://github.com/xtremevision,

If you feel like these failures are not due to your changes then try re-running the specific tests which are failing.

Thanks

— Reply to this email directly, view it on GitHub https://github.com/magento/magento2/issues/36908#issuecomment-1452130567, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABR3CESDCBFEGLWQSDKPRHLW2DBALANCNFSM6AAAAAAVFCEPWA. You are receiving this because you were mentioned.Message ID: @.***>

--

Michael Mussulis

CEO

Xtreme Vision Systems SRL

Reg Com: J40/23/03.01.2012

CUI: 29505826

mobilePhone

+ 40 721 052 013 <tel:+%201%20772%20418%203463>

mobilePhone

+ 1 772 418 3463 <tel:+%201%20772%20418%203463>

emailAddress

***@***.*** ***@***.***>

website

https://xtreme-vision.net <https://latinrhythms.dance>