owebia / magento2-module-advanced-shipping

Other
90 stars 28 forks source link

Incorrect shipping method when using rest API with token authentication #31

Closed aloross closed 5 years ago

aloross commented 7 years ago

Hello, we are using the Owebia Shipping extension in a Headless context and encountered an issue when fetching the available shipping methods using the rest API.

When we use an user token, if an Owebia method use a condition related to the quote information (like base_grand_total), it doesn't works properly.

The Owebia configuration is the following :

addMethod('not_free', [
    'title'      => "You will pay for that",
    'price'      => 12,
    'enabled'    => in_array($request->dest_country_id, ['FR', 'MC', 'AD']) && $quote->base_grand_total < 149.99,
]);

addMethod('free', [
    'title'      => "Free shipping!",
    'price'      => 0,
    'enabled'    => in_array($request->dest_country_id, ['FR', 'MC', 'AD']) && $quote->base_grand_total >= 149.99,
]);

Using a proper PHP session ID return the correct carrier method, like in the Magento original cart. You can see below two curl requests which demonstrate the difference between the calls :

 curl 'http://www.example.com/V1/carts/mine/estimate-shipping-methods' -H 'Cookie:PHPSESSID=user-php-session-id' -H 'X-Requested-With:XMLHttpRequest' -H 'Content-Type:application/json; charset=utf-8' --data-binary $'{"address":{"country_id":"FR"}}'
 curl 'http://www.example.com/rest/V1/carts/mine/estimate-shipping-methods' -H 'Authorization:Bearer user-token' -H 'X-Requested-With:XMLHttpRequest' -H 'Content-Type:application/json; charset=utf-8' --data-binary $'{"address":{"country_id":"FR"}}'

In the first case, everything works properly and i get the free method when my cart has a total superior to 150€. In the second case, i only get the first method.

I dove a little into the code and it seems that the quote is not initialized with the correct data in the token case. In the capture bellow, we can see that the evaluation of quote->base_grand_total return null.

owebia

Have you any idea what can be causing this issue ? Regards.

owebia commented 7 years ago

Hi,

The quote object is not passed to the carrier instance when shipping methods are requested. So we need to get it another way: we get quote from session data. https://github.com/owebia/magento2-module-advanced-setting-core/blob/master/Model/Wrapper/Quote.php

With a curl call, you doesn't specify the context so how can we get the quote without having a session id? It is not possible.

Best Regards,

aloross commented 7 years ago

Hi,

I totally get that, i just thought that you used some info in the request object to get back the quote (like using the quote items that are available here).

We used the request global which have the necessary information to match our use case and it works fine.

If I may, could you indicate in the documentation that some of the globals are not accessible in a headless context ?

Feel free to close this issue and have a nice day.

Regards.

owebia commented 7 years ago

i just thought that you used some info in the request object to get back the quote (like using the quote items that are available here).

I never thought to get the id of the quote that way, thanks for the idea, I'll see if it's feasible that way.

Regards, A.L.

real34 commented 6 years ago

Hi, is it still planned? Has there been any advancement on this?

owebia commented 6 years ago

See commits https://github.com/owebia/magento2-module-advanced-setting-core/commit/51d9259d9f0a428ac26622990f172a64225f1d94 and https://github.com/owebia/magento2-module-advanced-setting-core/commit/d494eae8c8c438db06a8c1aec72e07c3683bd554

owebia commented 5 years ago

Hi @adrien-louis-r and @real34 , This is fixed in 2.2.0 version of the module. Best Regards, A.L.

aloross commented 5 years ago

Hi @owebia,

Thanks for that!

Have a nice day, Adrien.