Skip to content

Commit

Permalink
Merge pull request spree#10480 from johannboutet/master
Browse files Browse the repository at this point in the history
API v2 Set order state to delivery if selected shipping rate is changed
  • Loading branch information
damianlegawiec authored Sep 21, 2020
2 parents d7b6c05 + 39ddce0 commit 7b64942
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
12 changes: 6 additions & 6 deletions api/spec/requests/spree/api/v2/storefront/checkout_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -276,9 +276,9 @@
let(:params) do
{
order: {
shipments_attributes: {
'0' => { selected_shipping_rate_id: new_selected_shipping_rate_id, id: shipment.id }
}
shipments_attributes: [
{ selected_shipping_rate_id: new_selected_shipping_rate_id, id: shipment.id }
]
}
}
end
Expand Down Expand Up @@ -667,9 +667,9 @@
let(:shipment_params) do
{
order: {
shipments_attributes: {
'0' => { selected_shipping_rate_id: shipping_rate_id, id: shipment_id }
}
shipments_attributes: [
{ selected_shipping_rate_id: shipping_rate_id, id: shipment_id }
]
}
}
end
Expand Down
10 changes: 9 additions & 1 deletion core/app/services/spree/checkout/update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ def call(order:, params:, permitted_attributes:, request_env:)
bill_changed = address_with_country_iso_present?(params, 'bill')
params = replace_country_iso_with_id(params, 'ship') if ship_changed
params = replace_country_iso_with_id(params, 'bill') if bill_changed
order.state = 'address' if (ship_changed || bill_changed) && order.checkout_steps.include?('address')
order.state = 'address' if (ship_changed || bill_changed) && order.has_checkout_step?('address')
order.state = 'delivery' if selected_shipping_rate_present?(params) && order.has_checkout_step?('delivery')
return success(order) if order.update_from_params(params, permitted_attributes, request_env)

failure(order)
Expand All @@ -23,6 +24,13 @@ def address_with_country_iso_present?(params, address_kind = 'ship')
true
end

def selected_shipping_rate_present?(params)
shipments_attributes = params.dig(:order, :shipments_attributes)
return false unless shipments_attributes

shipments_attributes.any? { |s| s.dig(:selected_shipping_rate_id) }
end

def replace_country_iso_with_id(params, address_kind = 'ship')
country_id = Spree::Country.by_iso(params[:order]["#{address_kind}_address_attributes"].fetch(:country_iso))&.id

Expand Down
30 changes: 30 additions & 0 deletions core/spec/services/spree/checkout/update_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,34 @@
expect(order.ship_address.state.id).to eq state.id
end
end

describe 'update selected shipping rate' do
let(:update_service) { described_class.new }
let(:order) { create(:order_with_line_items) }
let(:order_params) do
ActionController::Parameters.new(
order: {
shipments_attributes: [
{
id: order.shipments.first.id,
selected_shipping_rate_id: order.shipments.first.shipping_rates.first.id
}
]
}
)
end
let(:permitted_attributes) do
Spree::PermittedAttributes.checkout_attributes + [
shipments_attributes: Spree::PermittedAttributes.shipment_attributes
]
end

it 'should set order back to delivery state' do
expect(order.state).not_to eq 'delivery'

update_service.send(:call, order: order, params: order_params, permitted_attributes: permitted_attributes, request_env: nil)

expect(order.state).to eq 'delivery'
end
end
end

0 comments on commit 7b64942

Please sign in to comment.