Disable guest checkout in model

This commit is contained in:
Matt-Yorkley
2018-03-03 23:28:10 +00:00
committed by Maikel Linke
parent 6b2c4de20f
commit 358edb4727
2 changed files with 35 additions and 0 deletions

View File

@@ -17,6 +17,7 @@ Spree::Order.class_eval do
validates :customer, presence: true, if: :require_customer?
validate :products_available_from_new_distribution, :if => lambda { distributor_id_changed? || order_cycle_id_changed? }
validate :disallow_guest_order, if: lambda { using_guest_checkout? && registered_email? }
attr_accessible :order_cycle_id, :distributor_id, :customer_id
before_validation :shipping_address_from_distributor
@@ -82,6 +83,18 @@ Spree::Order.class_eval do
errors.add(:base, I18n.t(:spree_order_availability_error)) unless DistributionChangeValidator.new(self).can_change_to_distribution?(distributor, order_cycle)
end
def using_guest_checkout?
require_email && !user.andand.id
end
def registered_email?
Spree.user_class.find_by_email(email).present?
end
def disallow_guest_order
errors.add(:base, I18n.t('devise.failure.already_registered'))
end
def empty_with_clear_shipping_and_payments!
empty_without_clear_shipping_and_payments!
payments.clear

View File

@@ -673,6 +673,28 @@ describe Spree::Order do
end
end
describe "when a guest order is placed with a registered email" do
let(:order) { create(:order_with_totals_and_distribution, user: nil) }
let(:payment_method) { create(:payment_method, distributors: [order.distributor]) }
let(:shipping_method) { create(:shipping_method, distributors: [order.distributor]) }
let(:user) { create(:user, email: 'registered@email.com') }
before do
order.bill_address = create(:address)
order.ship_address = create(:address)
order.shipping_method = shipping_method
order.email = user.email
order.user = nil
order.state = 'cart'
end
it "returns a validation error" do
expect{order.next}.to change(order.errors, :count).from(0).to(1)
expect(order.errors.messages[:base]).to eq [ I18n.t('devise.failure.already_registered') ]
expect(order.state).to eq 'cart'
end
end
describe "a completed order with shipping and transaction fees" do
let(:distributor) { create(:distributor_enterprise, charges_sales_tax: true, allow_order_changes: true) }
let(:order) { create(:completed_order_with_fees, distributor: distributor, shipping_fee: shipping_fee, payment_fee: payment_fee) }