Skip to content

Commit 41f5bdf

Browse files
committed
Refactor shipping_rate calculation
The easypost_shipments were being created again upon retrieving shipping_rates. Hence modified estimator to check if an EasyPost shipment has already been created for the shipment. If it exists, it fetches the stored shipping rates instead of creating a new EasyPost shipment. Upon updating default shipping rate, all the other shipping rates of a shipment were being discarded. The change ensure that the newly selected shipping rate is set while also keeping the rest. Once the shipping rate is updated, the cost of shipment needs to be updated. Thereby reloading shipment to reflect any updates to the selected shipping rate also ensuring that the shipment cost is recalculated based on the latest selected shipping rate.
1 parent e6f2d36 commit 41f5bdf

File tree

4 files changed

+415
-4
lines changed

4 files changed

+415
-4
lines changed

app/decorators/models/solidus_easypost/spree/shipment_decorator.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,41 @@ def easypost_postage_label_url
2929
easypost_shipment&.postage_label&.label_url
3030
end
3131

32+
def select_shipping_method(shipping_method)
33+
# Selects the specified shipping method for the shipment.
34+
# This method fetches available shipping rates using the estimator and
35+
# marks the selected shipping rate while deselecting all others.
36+
estimator = ::Spree::Config.stock.estimator_class.new
37+
rates = estimator.shipping_rates(to_package, false)
38+
39+
# Find the rate that matches the provided shipping method
40+
rate = rates.detect { |detected| detected.shipping_method_id == shipping_method.id }
41+
42+
# Mark the selected shipping method as true and deselect others
43+
deselect_other_shipping_rates(rate.id)
44+
rate.update(selected: true)
45+
end
46+
47+
def update_amounts
48+
# NOTE: Reload the shipment to reflect any updates to the selected shipping rate.
49+
# Updates the shipment amounts by reloading the selected shipping rate.
50+
# This ensures that the shipment cost is recalculated based on the latest
51+
# selected shipping rate.
52+
reload
53+
54+
# Call the parent method from solidus_core to ensure proper cost recalculations.
55+
super
56+
end
57+
3258
private
3359

60+
def deselect_other_shipping_rates(selected_rate_id)
61+
# Deselects all shipping rates except the one selected by the user.
62+
# This ensures that only one shipping rate remains marked as selected.
63+
# Update all other shipping rates, setting `selected` to false.
64+
shipping_rates.where.not(id: selected_rate_id).update_all(selected: false)
65+
end
66+
3467
def buy_easypost_rate
3568
# Skip label purchase if tracking information already exists.
3669
return if tracking

lib/solidus_easypost/estimator.rb

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,22 @@
33
module SolidusEasypost
44
class Estimator
55
def shipping_rates(package, _frontend_only = true)
6-
easypost_rates = ShipmentBuilder.from_package(package).rates.sort_by(&:rate)
7-
8-
shipping_rates = easypost_rates.map { |rate| build_shipping_rate(rate) }.compact
9-
shipping_rates.min_by(&:cost)&.selected = true
6+
# NOTE: The easypost_shipments were being created again when retrieving shipping_rates
7+
# Hence this method has been updated to check if an EasyPost shipment has already been created for the shipment.
8+
# If it exists, it fetches the stored shipping rates instead of creating a new EasyPost shipment.
9+
shipment = package.shipment
10+
11+
if shipment.easypost_shipment
12+
shipping_rates = shipment.shipping_rates
13+
else
14+
easypost_rates = ShipmentBuilder.from_package(package).rates.sort_by(&:rate)
15+
16+
# Build shipping rate objects from EasyPost rates.
17+
shipping_rates = easypost_rates.map { |rate| build_shipping_rate(rate) }.compact
18+
19+
# Automatically select the shipping rate with the lowest cost.
20+
shipping_rates.min_by(&:cost)&.selected = true
21+
end
1022

1123
shipping_rates
1224
end

0 commit comments

Comments
 (0)