verbb / postie

A Craft Commerce shipping calculator plugin.
Other
12 stars 18 forks source link

EVENT_BEFORE_FETCH_RATES not triggering #141

Open apitel opened 3 months ago

apitel commented 3 months ago

Describe the bug

Postie is not triggering this EVENT_BEFORE_FETCH_RATES event when a cart is marked as completed to transition into order. Line vendor/verbb/postie/src/base/Provider.php:515 is not being reached because Postie does not see our plugin's EVENT_BEFORE_FETCH_RATES handler.

image

This is our code where we have define our EVENT_BEFORE_FETCH_RATES handler and it is being triggered whenever any order is being updated (but not when a cart is marked as completed):

image

Steps to reproduce

  1. Define Define EVENT_BEFORE_FETCH_RATES in custom plugin based on https://verbb.io/craft-plugins/postie/docs/developers/events#the-beforeFetchRates-event example
  2. Start creating a new Order through CP
  3. Press "Mark as completed"
  4. Notice that EVENT_BEFORE_FETCH_RATES is not triggered
  5. Re-save the same order and notice that EVENT_BEFORE_FETCH_RATES is triggered

Craft CMS version

Craft Pro 5.3.4

Plugin version

dev-craft-5

Multi-site?

Yes

Additional context

No response

engram-design commented 3 months ago

This would be more down to how Commerce handles registered shipping methods. We register our shipping methods via craft\commerce\services\ShippingMethods::EVENT_REGISTER_AVAILABLE_SHIPPING_METHODS which will then fetch all available shipping methods, include rate calls to the cache, or otherwise.

I wouldn't have thought a rate-fetch is done when marking an order as completed, because rates have already been fetched at that point (where you would select the shipping method during checkout). In fact, you wouldn't want rates to be changing at any other time, as they'd make for an unexpected experience (the rate could change).

There might need to be some other mechanism for your logic, if it depends on not only when a cart is saved, but when it's made into an order too? The Order::EVENT_AFTER_COMPLETE_ORDER for example.

apitel commented 3 months ago

We are using EVENT_BEFORE_FETCH_RATES to adjust the $payload which is sent to the FedEx's API. The issue is that the EVENT_BEFORE_FETCH_RATES is not being triggered when pressing (or before pressing) "Mark as completed" which means that our modified payload is not being sent to the FedEx's API and we are receiving Shipping Methods with wrong (for our case) shipping rates. It is important for us to be able to use EVENT_BEFORE_FETCH_RATES event also during the cart stage to receive the correct rate from FedEx because we are doing our own package consolidation and various settings modification within the payload. EVENT_BEFORE_FETCH_RATES event is working as expected once the order is in a completed stage.

engram-design commented 3 months ago

Using that event to modify what's sent to FedEx is totally fine. It was my understanding of Commerce that nothing is recalculated when completing an order, and hence no need to recalculate shipping methods. Happy to be proven wrong, and maybe this is new behavior in Commerce 5, which would be odd.

One final thing to ask is if you have the enableRouteCheck setting enabled? We're pretty protective against fetching shipping methods unless we have to, as Commerce fires them multiple times per requests. Maybe disabling that and testing it worth a shot as well.

I'll investigate!

apitel commented 3 months ago

I appreciate the quick responses! Here is our config (enableRouteCheck is disabled):

<?php

return [
    'pluginName' => 'Postie',
    'enableCaching' => false,
    'enableRouteCheck' => false,
    'shippedOrderStatus' => 'shipped',
    'partiallyShippedOrderStatus' => 'partiallyShipped',
];