class Stall::Checkout::InformationsCheckoutStep

Public Instance Methods

prepare() click to toggle source
# File lib/stall/checkout/informations_checkout_step.rb, line 22
def prepare
  ensure_customer
  prefill_addresses_from_customer
  ensure_shipment
  ensure_payment
end
process() click to toggle source
# File lib/stall/checkout/informations_checkout_step.rb, line 29
def process
  prepare_user_attributes
  prepare_addresses_attributes
  cart.assign_attributes(cart_params)

  return false unless valid?

  cart.save.tap do |valid|
    assign_addresses_to_customer!
    calculate_shipping_fee!
  end
end

Private Instance Methods

assign_addresses_to_customer!() click to toggle source

Copies the addresses filled in the cart to the customer account for next orders informations pre-filling

# File lib/stall/checkout/informations_checkout_step.rb, line 145
def assign_addresses_to_customer!
  Stall::Addresses::CopySourceToTarget.new(cart, cart.customer).copy!
end
calculate_shipping_fee!() click to toggle source

Assigns the shipping fees to the cart based on the selected shipping method

# File lib/stall/checkout/informations_checkout_step.rb, line 130
def calculate_shipping_fee!
  service_class = Stall.config.service_for(:shipping_fee_calculator)
  service_class.new(cart).call
end
cart_params(*attributes) click to toggle source

To easily add permitted parameters, use the following method in your subclass :

def cart_params
  super(customer_attributes: [nested: :attribute])
end

Note : If you want to remove permitted parameters, you need to rewrite the full permissions nested array

Calls superclass method Stall::Checkout::Step#cart_params
# File lib/stall/checkout/informations_checkout_step.rb, line 54
def cart_params(*attributes)
  @cart_params ||= super(
    *merge_arrays(
      [
        :use_another_address_for_billing, :terms,
        :payment_method_id, :shipping_method_id,
        customer_attributes: [
          :id, :email, user_attributes: [
            :password, :password_confirmation
          ]
        ],
        shipping_address_attributes: [
          :id, :civility, :first_name, :last_name, :address,
          :address_details, :country, :zip, :city, :phone, :state
        ],
        billing_address_attributes: [
          :id, :civility, :first_name, :last_name, :address,
          :address_details, :country, :zip, :city, :phone, :state,
          :_destroy
        ],
        shipment_attributes: [
          :id, :shipping_method_id
        ],
        payment_attributes: [
          :id, :payment_method_id
        ]
      ],
      attributes
    )
  )
end
customer_accepts_terms() click to toggle source
# File lib/stall/checkout/informations_checkout_step.rb, line 153
def customer_accepts_terms
  cart.errors.add(:terms, :accepted) unless cart.terms == '1'
end
ensure_customer() click to toggle source
# File lib/stall/checkout/informations_checkout_step.rb, line 86
def ensure_customer
  cart.build_customer unless cart.customer
end
ensure_payment() click to toggle source
# File lib/stall/checkout/informations_checkout_step.rb, line 94
def ensure_payment
  cart.build_payment unless cart.payment
end
ensure_shipment() click to toggle source
# File lib/stall/checkout/informations_checkout_step.rb, line 90
def ensure_shipment
  cart.build_shipment unless cart.shipment
end
prefill_addresses_from_customer() click to toggle source

Fetches addresses from the customer account and copy them to the cart to pre-fill the fields for the user

# File lib/stall/checkout/informations_checkout_step.rb, line 138
def prefill_addresses_from_customer
  Stall::Addresses::PrefillTargetFromSource.new(cart.customer, cart).copy
end
prepare_addresses_attributes() click to toggle source

If the billing address should be set to the same as the filled shipping address, we remove the billing address parameters

# File lib/stall/checkout/informations_checkout_step.rb, line 120
def prepare_addresses_attributes
  unless use_another_address_for_billing?
    cart_params.delete(:billing_address_attributes)
    cart.billing_address.try(:mark_for_destruction) if cart.billing_address?
  end
end
prepare_user_attributes() click to toggle source

Remvove user attributes when no account should be created, for an “anonymous” order creation.

# File lib/stall/checkout/informations_checkout_step.rb, line 101
def prepare_user_attributes
  return if params[:create_account] == '1'

  if cart_params[:customer_attributes] && cart_params[:customer_attributes][:user_attributes]
    cart_params[:customer_attributes].delete(:user_attributes)
  end

  # Remove user from customer to avoid automatic validation of the user
  # if no user should be saved with the customer
  unless user_signed_in? || cart.customer.try(:user).try(:persisted?) ||
    !cart.customer
  then
    cart.customer.user = nil
  end
end
use_another_address_for_billing?() click to toggle source
# File lib/stall/checkout/informations_checkout_step.rb, line 149
def use_another_address_for_billing?
  @use_another_address_for_billing ||= cart_params[:use_another_address_for_billing] == '1'
end