Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Stop calling deprecated fulfillment methods internally

Damian Legawiec9d3a522dd07a
diff --git a/spree/api/app/controllers/spree/api/v3/admin/orders/fulfillments_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/orders/fulfillments_controller.rb
index 88918ed78b..fdfb6cbfa7 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/orders/fulfillments_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/orders/fulfillments_controller.rb
@@ -58,7 +58,7 @@ module Spree
# PATCH /api/v3/admin/orders/:order_id/fulfillments/:id/fulfill
def fulfill
with_order_lock do
- @resource.ship!
+ @resource.fulfill!
render json: serialize_resource(@resource.reload)
rescue StateMachines::InvalidTransition => e
render_service_error(e.message)
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/orders/fulfillments_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/orders/fulfillments_controller_spec.rb
index 3565093070..616aa71881 100644
--- a/spree/api/spec/controllers/spree/api/v3/admin/orders/fulfillments_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/admin/orders/fulfillments_controller_spec.rb
@@ -75,7 +75,7 @@ RSpec.describe Spree::Api::V3::Admin::Orders::FulfillmentsController, type: :con
expect(response).to have_http_status(:created)
fulfillment = order.reload.shipments.first
- expect(fulfillment.shipping_method).to eq(delivery_method)
+ expect(fulfillment.delivery_method).to eq(delivery_method)
end
it "registers an already-shipped fulfillment with status: 'shipped' and an explicit cost" do
diff --git a/spree/core/app/models/spree/fulfillment_changer.rb b/spree/core/app/models/spree/fulfillment_changer.rb
index d17121991d..48ecca19d1 100644
--- a/spree/core/app/models/spree/fulfillment_changer.rb
+++ b/spree/core/app/models/spree/fulfillment_changer.rb
@@ -146,7 +146,7 @@ module Spree
end
def current_shipment_not_already_shipped
- return unless current_shipment.shipped?
+ return unless current_shipment.fulfilled?
errors.add(:current_shipment, :has_already_been_shipped)
end
diff --git a/spree/core/app/models/spree/order_inventory.rb b/spree/core/app/models/spree/order_inventory.rb
index 68d3d858d7..ddc31371f6 100644
--- a/spree/core/app/models/spree/order_inventory.rb
+++ b/spree/core/app/models/spree/order_inventory.rb
@@ -120,7 +120,7 @@ module Spree
end
def remove_from_shipment(shipment, quantity)
- return 0 if quantity.zero? || shipment.shipped?
+ return 0 if quantity.zero? || shipment.fulfilled?
shipment_units = shipment.inventory_units_for_item(line_item, variant).reject(&:shipped?).sort_by(&:status)
diff --git a/spree/core/app/services/spree/checkout/advance.rb b/spree/core/app/services/spree/checkout/advance.rb
index 2ad7a90219..2602ee68aa 100644
--- a/spree/core/app/services/spree/checkout/advance.rb
+++ b/spree/core/app/services/spree/checkout/advance.rb
@@ -20,13 +20,39 @@ module Spree
if shipping_method_id.present? && cart.fulfillments.count == 1 &&
cart.fulfillments.first.delivery_method&.id != shipping_method_id
- result = Spree::Checkout::SelectShippingMethod.call(order: cart, params: { shipping_method_id: shipping_method_id })
+ result = select_delivery_method(cart, shipping_method_id)
return result if result.failure?
end
cart.recalculate_totals!
success(cart)
end
+
+ private
+
+ # Quick-checkout convenience: wallets (Google Pay) send the chosen
+ # method alongside the address instead of as a separate selection, so
+ # the rate is picked here. Normal checkout selects a rate directly on
+ # the fulfillment.
+ def select_delivery_method(cart, delivery_method_id)
+ delivery_method = Spree::DeliveryMethod.find_by(id: delivery_method_id)
+ return failure(:delivery_method_not_found) if delivery_method.nil?
+
+ fulfillment = cart.fulfillments.first
+ rate = fulfillment.delivery_rates.find_by(delivery_method: delivery_method)
+
+ if rate.nil?
+ return failure(
+ :selected_delivery_method_not_found,
+ "Couldn't find delivery rates for Delivery Method with ID = #{delivery_method.id} and Fulfillment with ID = #{fulfillment.id}"
+ )
+ end
+
+ # Assigning the rate updates the fulfillment amount and order totals.
+ fulfillment.selected_delivery_rate_id = rate.id
+
+ success(fulfillment)
+ end
end
end
end
diff --git a/spree/core/app/services/spree/fulfillments/create.rb b/spree/core/app/services/spree/fulfillments/create.rb
index 1b5c973f6c..6ecdd6d941 100644
--- a/spree/core/app/services/spree/fulfillments/create.rb
+++ b/spree/core/app/services/spree/fulfillments/create.rb
@@ -224,7 +224,7 @@ module Spree
next unless shipment.fulfillment_items.sum(:quantity).zero?
inherited[:cost] += shipment.cost
- inherited[:delivery_method] ||= shipment.shipping_method if capture_delivery_method
+ inherited[:delivery_method] ||= shipment.delivery_method if capture_delivery_method
shipment.destroy!
end
@@ -241,7 +241,7 @@ module Spree
method = delivery_method || inherited[:delivery_method]
fulfillment.update_columns(cost: effective_cost) if effective_cost.positive?
- fulfillment.add_shipping_method(method, true) if method
+ fulfillment.add_delivery_method(method, true) if method
end
# Strict decimal parsing (same semantics as Shipment#cost=, which the
diff --git a/spree/core/spec/models/spree/order_updater_spec.rb b/spree/core/spec/models/spree/order_updater_spec.rb
index 23187c7566..999e43b48c 100644
--- a/spree/core/spec/models/spree/order_updater_spec.rb
+++ b/spree/core/spec/models/spree/order_updater_spec.rb
@@ -263,8 +263,8 @@ module Spree
include_context 'with original shipping method gone backend only'
let(:order) { create(:completed_order_with_totals) }
- it 'keeps the original shipping method' do
- expect { updater.update_shipments }.not_to change { order.fulfillments.first.shipping_method }
+ it 'keeps the original delivery method' do
+ expect { updater.update_shipments }.not_to change { order.fulfillments.first.delivery_method }
end
end
end
diff --git a/spree/core/spec/services/spree/checkout/select_shipping_method_spec.rb b/spree/core/spec/services/spree/checkout/select_shipping_method_spec.rb
index e3fe3bedfc..8696747e3b 100644
--- a/spree/core/spec/services/spree/checkout/select_shipping_method_spec.rb
+++ b/spree/core/spec/services/spree/checkout/select_shipping_method_spec.rb
@@ -62,7 +62,7 @@ module Spree
it 'sets selected shipping method for shipment' do
expect(shipment.shipping_rates.count).to eq(2)
execute
- expect(selected_shipping_rate.shipping_method).to eq(shipping_method_2)
+ expect(selected_shipping_rate.delivery_method).to eq(shipping_method_2)
end
end
@@ -103,11 +103,11 @@ module Spree
it 'sets selected shipping method for the specified shipment' do
expect(order.shipments.count).to eq(2)
- expect(shipment.selected_shipping_rate.shipping_method).to eq(shipping_method)
- expect(shipment_2.selected_shipping_rate.shipping_method).to eq(shipping_method)
+ expect(shipment.selected_shipping_rate.delivery_method).to eq(shipping_method)
+ expect(shipment_2.selected_shipping_rate.delivery_method).to eq(shipping_method)
execute
- expect(shipment.reload.selected_shipping_rate.shipping_method).to eq(shipping_method)
- expect(shipment_2.reload.selected_shipping_rate.shipping_method).to eq(shipping_method_2)
+ expect(shipment.reload.selected_shipping_rate.delivery_method).to eq(shipping_method)
+ expect(shipment_2.reload.selected_shipping_rate.delivery_method).to eq(shipping_method_2)
end
end
@@ -116,11 +116,11 @@ module Spree
it 'sets selected shipping method for all shipments' do
expect(order.shipments.count).to eq(2)
- expect(shipment.selected_shipping_rate.shipping_method).to eq(shipping_method)
- expect(shipment_2.selected_shipping_rate.shipping_method).to eq(shipping_method)
+ expect(shipment.selected_shipping_rate.delivery_method).to eq(shipping_method)
+ expect(shipment_2.selected_shipping_rate.delivery_method).to eq(shipping_method)
execute
- expect(shipment.reload.selected_shipping_rate.shipping_method).to eq(shipping_method_2)
- expect(shipment_2.reload.selected_shipping_rate.shipping_method).to eq(shipping_method_2)
+ expect(shipment.reload.selected_shipping_rate.delivery_method).to eq(shipping_method_2)
+ expect(shipment_2.reload.selected_shipping_rate.delivery_method).to eq(shipping_method_2)
end
end
end
diff --git a/spree/core/spec/services/spree/fulfillments/create_spec.rb b/spree/core/spec/services/spree/fulfillments/create_spec.rb
index c4836155c6..cde5f47175 100644
--- a/spree/core/spec/services/spree/fulfillments/create_spec.rb
+++ b/spree/core/spec/services/spree/fulfillments/create_spec.rb
@@ -102,15 +102,15 @@ module Spree
it 'keeps the delivery method selected through the rate refresh' do
expect(execute.success?).to eq(true)
- expect(fulfillment.shipping_method).to eq(delivery_method)
+ expect(fulfillment.delivery_method).to eq(delivery_method)
end
it "inherits the drained source's delivery method when none is given" do
- original_method = source_shipment.shipping_method
+ original_method = source_shipment.delivery_method
result = subject.call(order: order, stock_location: stock_location)
expect(result.success?).to eq(true)
- expect(result.value.shipping_method).to eq(original_method)
+ expect(result.value.delivery_method).to eq(original_method)
end
it 'inherits the first non-nil method when draining sources with different carriers' do
@@ -118,13 +118,13 @@ module Spree
second_source = order.shipments.create!(stock_location: stock_location)
second_source.add_shipping_method(other_method, true)
line_items.last.inventory_units.update_all(shipment_id: second_source.id)
- first_source_method = source_shipment.shipping_method
+ first_source_method = source_shipment.delivery_method
result = subject.call(order: order, stock_location: stock_location, status: 'shipped')
expect(result.success?).to eq(true)
expect(order.reload.shipments).to contain_exactly(result.value)
- expect(result.value.shipping_method).to eq(first_source_method)
+ expect(result.value.delivery_method).to eq(first_source_method)
end
it 'does not inherit a method from partially drained sources' do
@@ -136,7 +136,7 @@ module Spree
)
expect(result.success?).to eq(true)
- expect(result.value.shipping_method).to be_nil
+ expect(result.value.delivery_method).to be_nil
end
end
@@ -211,7 +211,7 @@ module Spree
expect(execute.success?).to eq(true)
expect(fulfillment.cost).to eq(source_shipment_cost)
expect(fulfillment.selected_shipping_rate.cost).to eq(source_shipment_cost)
- expect(fulfillment.selected_shipping_rate.shipping_method).to eq(delivery_method)
+ expect(fulfillment.selected_shipping_rate.delivery_method).to eq(delivery_method)
expect(order.reload.total).to eq(original_total)
end