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.5k stars 9.3k forks source link

[2.4.5] Incorrect carrier/method code on selected shipping method in GraphQL #36606

Closed DuckThom closed 1 year ago

DuckThom commented 1 year ago

Preconditions and environment

Steps to reproduce

  1. Set a shipping address on the cart which has an available shipping method with an underscore in the carrier (ie. sendcloud_checkout)
  2. Set the shipping method on the cart
  3. Compare the carrier and method codes in available_shipping_methods and selected_shipping_method on the cart.shipping_addresses[0] object returned by GraphQL

GraphQL query:

query {
    cart(cart_id: "<cart_id>") {
        shipping_addresses {
            selected_shipping_method {
                carrier_code
                method_code
            }
            available_shipping_methods {
                carrier_code
                method_code
            }
        }
    }
}

Expected result

The returned selected_shipping_method matches with one of the available_shipping_methods carrier and method codes.

Result based on above query:

{
    "data": {
        "cart": {
            "shipping_addresses": [
                {
                    "selected_shipping_method": {
                        "carrier_code": "sendcloud_checkout",
                        "method_code": "ddc961a8-1824-40ab-88a9-392f2f58e3b5"
                    },
                    "available_shipping_methods": [
                        {
                            "carrier_code": "sendcloud_checkout",
                            "method_code": "ddc961a8-1824-40ab-88a9-392f2f58e3b5"
                        }
                    ]
                }
            ]
        }
    }
}

Actual result

There is a mismatch between the carrier/method codes in the selectedShippingMethod and availableShippingMethods.

Result based on above query:

{
    "data": {
        "cart": {
            "shipping_addresses": [
                {
                    "selected_shipping_method": {
                        "carrier_code": "sendcloud",
                        "method_code": "checkout_ddc961a8-1824-40ab-88a9-392f2f58e3b5"
                    },
                    "available_shipping_methods": [
                        {
                            "carrier_code": "sendcloud_checkout",
                            "method_code": "ddc961a8-1824-40ab-88a9-392f2f58e3b5"
                        }
                    ]
                }
            ]
        }
    }
}

Additional information

The shipping method is saved on the quote as method.carrierCode + '_' + method.methodCode

In the resolver for the selected_shipping_method field, the following line is present:

https://github.com/magento/magento2/blob/ea0cf6395753078e45af8c4a5ddc1ea2c4d91092/app/code/Magento/QuoteGraphQl/Model/Resolver/ShippingAddress/SelectedShippingMethod.php#L53

This will split on the first underscore, which is not correct.

Possible solution:

diff --git a/vendor/magento/module-quote-graph-ql/Model/Resolver/ShippingAddress/SelectedShippingMethod.php b/vendor/magento/module-quote-graph-ql/Model/Resolver/ShippingAddress/SelectedShippingMethod.php
--- a/vendor/magento/module-quote-graph-ql/Model/Resolver/ShippingAddress/SelectedShippingMethod.php    
+++ b/vendor/magento/module-quote-graph-ql/Model/Resolver/ShippingAddress/SelectedShippingMethod.php
@@ -38,11 +38,11 @@
             return null;
         }

-        list($carrierCode, $methodCode) = explode('_', $address->getShippingMethod(), 2);
-
         /** @var Rate $rate */
         foreach ($rates as $rate) {
             if ($rate->getCode() == $address->getShippingMethod()) {
+                $carrierCode = $rate->getCarrier();
+                $methodCode = $rate->getMethod();
                 $carrierTitle = $rate->getCarrierTitle();
                 $methodTitle = $rate->getMethodTitle();
                 break;

Release note

No response

Triage and priority

m2-assistant[bot] commented 1 year ago

Hi @DuckThom. Thank you for your report. To speed up processing of this issue, make sure that you provided the following information:

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:

@magento give me 2.4-develop instance - upcoming 2.4.x release

For more details, review the Magento Contributor Assistant documentation.

Add a comment to assign the issue: @magento I am working on this

To learn more about issue processing workflow, refer to the Code Contributions.


:clock10: You can find the schedule on the Magento Community Calendar page.

:telephone_receiver: The triage of issues happens in the queue order. If you want to speed up the delivery of your contribution, join the Community Contributions Triage session to discuss the appropriate ticket.

:pencil2: Feel free to post questions/proposals/feedback related to the Community Contributions Triage process to the corresponding Slack Channel

m2-assistant[bot] commented 1 year ago

Hi @engcom-November. 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-November commented 1 year ago

Hi @DuckThom , Thank you for reporting and collaboration. As per Magento devdocs , Graphql supports only below shipping methods by default. image and none of the shipping methods have underscore in carrier code to reproduce the issue. Also sendcloud is a 3rd party package and code of the extension is not part of Magento git repository. Community cannot able to provide fix for it in this repository. All questions, issue reports and fix for them should be addressed to the corresponding extension owners(support) on the Magento Market place page. Kindly recheck the issue on Magento 2.4-develop instance with Graphql supported shipping methods and provide missing steps if the issue is still reproducible. Thank you.

DuckThom commented 1 year ago

I agree that the built-in carriers do not use underscores in the codes, however, it's not mentioned anywhere what the allowed characters are for custom carriers.

IMO, it would be better if it was either mentioned in the dev docs what the allowed characters are or if some validation is added perhaps. As right now, it can lead to cases such as this one where unexpected data is returned.

engcom-November commented 1 year ago

Hi @DuckThom , Verified the issue again on Magento 2.4-develop instance using custom shipping method using both with / without underscore in carrier code and the issue is reproducible. Hence updating the description and confirming the issue. Scenario 1: Custom shipping method created without underscore in carrier code. No issue. Graphql response retrieved as expected. Method displayed in "available_shipping_methods" and "selected_shipping_method" image

Scenario 2: Custom shipping method created with underscore in carrier code. Issue: Underscore is missing in Graphql response and the method is not displayed in "available_shipping_methods". image

github-jira-sync-bot commented 1 year ago

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

m2-assistant[bot] commented 1 year ago

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

github-jira-sync-bot commented 1 year ago

:x: You don't have permission to export this issue.

Vasudev-22 commented 1 year ago

@magento I am working on this

engcom-November commented 1 year ago

Hi @DuckThom , Development team is currently working on this issue. Thank you.

engcom-Hotel commented 1 year ago

Hello,

As I can see this issue got fixed in the scope of the internal Jira ticket AC-7550 by the internal team Related commits: https://github.com/magento/magento2/search?q=AC-7550&type=commits

Based on the Jira ticket, the target version is 2.4.7-beta1.

Thanks

hostep commented 1 year ago

@engcom-Hotel: either you made a typo, or the info from Jira is incorrect, because those fixes are not included in Magento 2.4.6? It's probably going to be included in 2.4.7(-beta1)?

engcom-Hotel commented 1 year ago

Hello @hostep,

That was a typo, I corrected it.

Thanks