Description
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
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.
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
- Severity: S0 - Affects critical data or functionality and leaves users without workaround.
- Severity: S1 - Affects critical data or functionality and forces users to employ a workaround.
- Severity: S2 - Affects non-critical data or functionality and forces users to employ a workaround.
- Severity: S3 - Affects non-critical data or functionality and does not force users to employ a workaround.
- Severity: S4 - Affects aesthetics, professional look and feel, “quality” or “usability”.
Metadata
Metadata
Assignees
Labels
Type
Projects
Status