mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-30 21:27:17 +00:00
Save default addresses when requested during checkout
This commit is contained in:
@@ -30,7 +30,7 @@ module Spree
|
||||
go_to_state :complete
|
||||
end
|
||||
|
||||
attr_accessor :use_billing, :checkout_processing
|
||||
attr_accessor :use_billing, :checkout_processing, :save_bill_address, :save_ship_address
|
||||
|
||||
token_resource
|
||||
|
||||
@@ -104,6 +104,8 @@ module Spree
|
||||
before_save :update_shipping_fees!, if: :complete?
|
||||
before_save :update_payment_fees!, if: :complete?
|
||||
|
||||
after_save_commit :save_default_addresses
|
||||
|
||||
# -- Scopes
|
||||
scope :not_empty, -> {
|
||||
left_outer_joins(:line_items).where.not(spree_line_items: { id: nil })
|
||||
@@ -743,5 +745,22 @@ module Spree
|
||||
|
||||
pending_payments.first.update_attribute :amount, total
|
||||
end
|
||||
|
||||
def save_default_addresses
|
||||
return unless save_bill_address || save_ship_address
|
||||
|
||||
if save_bill_address
|
||||
customer.bill_address_id = bill_address_id
|
||||
user&.bill_address_id = bill_address_id
|
||||
end
|
||||
|
||||
if save_ship_address
|
||||
customer.ship_address_id = ship_address_id
|
||||
user&.ship_address_id = ship_address_id
|
||||
end
|
||||
|
||||
customer.save
|
||||
user&.save
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -24,6 +24,7 @@ module Checkout
|
||||
def apply_strong_parameters
|
||||
@order_params = params.require(:order).permit(
|
||||
:email, :shipping_method_id, :special_instructions, :existing_card_id,
|
||||
:save_bill_address, :save_ship_address,
|
||||
bill_address_attributes: ::PermittedAttributes::Address.attributes,
|
||||
ship_address_attributes: ::PermittedAttributes::Address.attributes,
|
||||
payments_attributes: [
|
||||
|
||||
@@ -60,8 +60,8 @@
|
||||
|
||||
- if spree_current_user||true
|
||||
%div.checkout-input
|
||||
= f.check_box :checkout_default_bill_address
|
||||
= f.label :checkout_default_bill_address, t(:checkout_default_bill_address)
|
||||
= f.check_box :save_bill_address
|
||||
= f.label :save_bill_address, t(:checkout_default_bill_address)
|
||||
|
||||
%div.checkout-substep{ "data-controller": "toggle shippingmethod" }
|
||||
- selected_shipping_method = @order.shipping_method&.id || params[:shipping_method_id]
|
||||
@@ -136,8 +136,8 @@
|
||||
|
||||
- if spree_current_user
|
||||
%div.checkout-input{ "data-toggle-target": "content", style: "display: none" }
|
||||
= f.check_box :default_ship_address, { id: "default_ship_address", name: "default_ship_address" }
|
||||
= f.label :default_ship_address, t(:checkout_default_ship_address), { for: "default_ship_address" }
|
||||
= f.check_box :save_ship_address
|
||||
= f.label :save_ship_address, t(:checkout_default_ship_address)
|
||||
|
||||
.div.checkout-input
|
||||
= f.label :special_instructions, t(:checkout_instructions)
|
||||
|
||||
@@ -82,6 +82,22 @@ describe SplitCheckoutController, type: :controller do
|
||||
expect(response).to redirect_to checkout_step_path(:payment)
|
||||
expect(order.reload.state).to eq "payment"
|
||||
end
|
||||
|
||||
describe "saving default addresses" do
|
||||
it "updates default bill address on user and customer" do
|
||||
put :update, params: params.merge({ order: { save_bill_address: true } })
|
||||
|
||||
expect(order.customer.bill_address).to eq(order.bill_address)
|
||||
expect(order.user.bill_address).to eq(order.bill_address)
|
||||
end
|
||||
|
||||
it "updates default ship address on user and customer" do
|
||||
put :update, params: params.merge({ order: { save_ship_address: true } })
|
||||
|
||||
expect(order.customer.ship_address).to eq(order.ship_address)
|
||||
expect(order.user.ship_address).to eq(order.ship_address)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user