Sha256: ae7c065a707b9fe0ca51e1a89e943cf30526d32cab465b4a28d417c8c2b4e038

Contents?: true

Size: 1.05 KB

Versions: 1

Compression:

Stored size: 1.05 KB

Contents

module Spree
  CheckoutController.class_eval do

    private

    def before_address
      return if @order.bill_address or @order.ship_address

      last_used_bill_address, last_used_ship_address = find_last_used_addresses(@order.email)

      if spree_current_user.respond_to?(:bill_address) && spree_current_user.respond_to?(:ship_address)
        preferred_bill_address, preferred_ship_address = spree_current_user.bill_address, spree_current_user.ship_address
      end

      @order.bill_address ||= preferred_bill_address || last_used_bill_address || Address.default
      @order.ship_address ||= preferred_ship_address || last_used_ship_address || Address.default
    end

    def find_last_used_addresses(email)
      past = Spree::Order.order(id: :desc).where(email: email).where.not(state: 'cart').limit(8)
      if order = past.detect(&:bill_address)
        bill_address = order.bill_address.clone if order.bill_address
        ship_address = order.ship_address.clone if order.ship_address
      end
      return bill_address, ship_address
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
spree-last-address-2.2.0 app/controllers/spree/checkout_controller_decorator.rb