Currently, we don't have an "easy" way of calculating shipping rates for a shipment.
This is currently done inside the ShippingRateOptionsBuilder, which is definitely not ideal and it kind of feels weird to call that service in an API context for example.
Additionally, we currently do not allow altering the shipping rates returned, and it wouldn't make sense to fire an event from there.
I've been thinking about several different ways to improve this and first thought about introducing a calculateRates() method to shipments, but entity classes are supposed to be "dumb" and only contains minimal logic next to the getters/setters.
Instead, I'm introducing a new service called the "ShipmentManager", that currently has a single method for calculating rates, and I'm firing an events from there, for altering the rates, which would solve #2991416: Add an event for altering the calculated shipping rates.
The event for altering rates was largely copied from there, and I modified it a bit to also accept a $shipment.
Since there's currently no setter for setting the amount on the ShippingRate object, I modified the ShippingRate class and added 2 new methods:
fromArray() and toArray().
This way, when writing a subscriber, if the amount needs to be altered, it can be done using the following code:
$rate_values = $rate->toArray();
$rate_values['amount'] = new Price('200.00', 'USD');
$rate = ShippingRate::fromArray($rate_values);
$event->setRates([$rate]);
| Comment | File | Size | Author |
|---|---|---|---|
| #14 | commerce_shipping-3098140-14.patch | 12.71 KB | jsacksick |
Comments
Comment #2
jsacksick commentedComment #3
jsacksick commentedComment #4
jsacksick commentedSome notes related to the patch, an event is fired for each shipping method, wondering if this has a performance impact or not.
The reason for that is that there would be no easy way to access the shipping method otherwise (There's no $shipping_rate->getShippingMethod() for instance).
Comment #5
bojanz commentedThis feels correct, but unnecessarily long. Do we lose anything by just going with const RATES = 'commerce_shipping.rates' and documenting that it's used to alter calculated rates?
You can't add a return typehint if NULL is a possible value, because we need to support PHP 7.0, which has no nullable typehints. In case $this->deliveryDate is NULL you will get a crash.
Comment #6
jsacksick commentedComment #7
jsacksick commentedOops!
Comment #8
mglamanSo I thought return types were not allowed <= 7.1? but tests are passing?
Total side quest incoming: I feel like we should have a ShippingMethod repository, with loadAvailable which takes a shipment. Instead of using a storage.
👍
Return hints require PHP7.2 :O
Unfortunately. And Commerce is requiring 7.1 only.
🥳🎉
Isn't this data available from the rate object without having to split the rate ID? Less changes later if we deprecate the rate ID.
Comment #9
mglamanLooks like we should also consider #3017776: Exception in any shipping handler causes shipping to fail for all methods here.
Comment #10
jsacksick commentedValid point, perhaps to threat in a separate issue?
Regarding the return hints, removing them, we also need to remove the ones added in ShippingRateOption by me :p.
Regarding #3017776: Exception in any shipping handler causes shipping to fail for all methods, wondering if we shouldn't re-roll the patch it once this gets in?
I agree on this one, but unfortunately, the ShippingRate doesn't have access to the Shipping Method, so wondering how we should be handling this...
Comment #11
bojanz commentedBumping back to needs work since typehints were mistakenly removed, PHP 7.0 supports them and Commerce 2.15 has them.
Pasting yesterday's notes on the needed solution:
#3092772: Give plugins access to the full parent config entity, not just its ID in Commerce, which gives us $this->entity in ShippingMethodBase. Have that passed to ShippingRate (most likely a BC break because we'll change __construct), so that we have $rate->getShippingMethod(). I would also make $rate->getId() return $shipping_method->id() . '--' . $service. Thus removing any need for ShippingRateOption and ShippingRateOptionsBuilder. All of this is followup material.
Comment #12
jsacksick commentedSo then we're back to the patch from comment #7, anything else that needs to be added before we commit this?
Comment #13
bojanz commentedCommitted #2991416: Add an event for altering the calculated shipping rates, containing relevant portions of #10.
This now needs a reroll.
Comment #14
jsacksick commentedAttached the patch rerolled, I'm pretty sure you'd like to take care of #11 before committing it that (leaving that to you), but at least the patch applies again.
Comment #16
bojanz commentedCommitted #14.
I'm sad that we ended up abandoning the typehints we initially added to ShippingRate. Let's return to that in a followup.
#11 is also followup material cause it will be a BC break.
We now have ShippingOrderManager and ShipmentManager. Both are thin wrappers over underlying APIs, and they'll only get thinner over time (thanks to #11, for example). It might make sense in the future to admit that it's one giant facade, and unite the classes into ShippingManager. That will be up to mglaman and jacksick to decide.