Destroy incomplete orders if they can't be merged, prevents that order unexpectedly appearing in cart after checkout. Also ensure all orders have a shipping method.

This commit is contained in:
Rohan Mitchell
2012-10-05 12:09:53 +10:00
parent 6d9257d941
commit 568d948cbe
5 changed files with 99 additions and 86 deletions

View File

@@ -3,15 +3,19 @@ module Spree
# Override definition in spree/auth/app/controllers/spree/base_controller_decorator.rb
# Do not attempt to merge incomplete and current orders when they have differing distributors
# Instead, destroy the incomplete orders, otherwise they are restored after checkout, causing much confusion
def set_current_order
if current_user
if current_user.respond_to?(:last_incomplete_order)
last_incomplete_order = current_user.last_incomplete_order
if session[:order_id].nil? && last_incomplete_order
session[:order_id] = last_incomplete_order.id
elsif current_order && last_incomplete_order && current_order != last_incomplete_order &&
(current_order.distributor.nil? || current_order.distributor == last_incomplete_order.distributor)
current_order.merge!(last_incomplete_order)
elsif current_order && last_incomplete_order && current_order != last_incomplete_order
if current_order.distributor.nil? || current_order.distributor == last_incomplete_order.distributor
current_order.merge!(last_incomplete_order)
else
last_incomplete_order.destroy
end
end
end
end

View File

@@ -1,6 +1,7 @@
Spree::Order.class_eval do
belongs_to :distributor
before_validation :shipping_address_from_distributor
after_create :set_default_shipping_method
@@ -30,9 +31,6 @@ Spree::Order.class_eval do
end
before_validation :shipping_address_from_distributor
private
# On creation of the order (when the first item is added to the user's cart), set the

View File

@@ -0,0 +1,10 @@
class PopulateOrderDefaultShippingMethod < ActiveRecord::Migration
def up
Spree::Order.where(shipping_method_id: nil).each do |order|
order.send(:set_default_shipping_method)
end
end
def down
end
end

View File

@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20120919013335) do
ActiveRecord::Schema.define(:version => 20121005015852) do
create_table "distributors", :force => true do |t|
t.string "name"
@@ -23,8 +23,8 @@ ActiveRecord::Schema.define(:version => 20120919013335) do
t.string "abn"
t.string "acn"
t.string "description"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.integer "pickup_address_id"
t.string "next_collection_at"
end
@@ -65,8 +65,8 @@ ActiveRecord::Schema.define(:version => 20120919013335) do
t.string "alternative_phone"
t.integer "state_id"
t.integer "country_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.string "company"
end
@@ -75,12 +75,12 @@ ActiveRecord::Schema.define(:version => 20120919013335) do
create_table "spree_adjustments", :force => true do |t|
t.integer "source_id"
t.decimal "amount", :precision => 8, :scale => 2
t.decimal "amount", :precision => 8, :scale => 2, :default => 0.0
t.string "label"
t.string "source_type"
t.integer "adjustable_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.boolean "mandatory"
t.boolean "locked"
t.integer "originator_id"
@@ -112,15 +112,15 @@ ActiveRecord::Schema.define(:version => 20120919013335) do
t.string "type"
t.integer "calculable_id", :null => false
t.string "calculable_type", :null => false
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "spree_configurations", :force => true do |t|
t.string "name"
t.string "type", :limit => 50
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "spree_configurations", ["name", "type"], :name => "index_configurations_on_name_and_type"
@@ -133,7 +133,7 @@ ActiveRecord::Schema.define(:version => 20120919013335) do
t.integer "numcode"
end
create_table "spree_creditcards", :force => true do |t|
create_table "spree_credit_cards", :force => true do |t|
t.string "month"
t.string "year"
t.string "cc_type"
@@ -158,8 +158,8 @@ ActiveRecord::Schema.define(:version => 20120919013335) do
t.string "environment", :default => "development"
t.string "server", :default => "test"
t.boolean "test_mode", :default => true
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "spree_inventory_units", :force => true do |t|
@@ -167,8 +167,8 @@ ActiveRecord::Schema.define(:version => 20120919013335) do
t.string "state"
t.integer "variant_id"
t.integer "order_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.integer "shipment_id"
t.integer "return_authorization_id"
end
@@ -182,8 +182,8 @@ ActiveRecord::Schema.define(:version => 20120919013335) do
t.integer "variant_id"
t.integer "quantity", :null => false
t.decimal "price", :precision => 8, :scale => 2, :null => false
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.integer "max_quantity"
t.integer "shipping_method_id"
end
@@ -195,22 +195,22 @@ ActiveRecord::Schema.define(:version => 20120919013335) do
t.integer "source_id"
t.string "source_type"
t.text "details"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "spree_mail_methods", :force => true do |t|
t.string "environment"
t.boolean "active", :default => true
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "spree_option_types", :force => true do |t|
t.string "name", :limit => 100
t.string "presentation", :limit => 100
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.integer "position", :default => 0, :null => false
end
@@ -224,8 +224,8 @@ ActiveRecord::Schema.define(:version => 20120919013335) do
t.string "name"
t.string "presentation"
t.integer "option_type_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "spree_option_values_variants", :id => false, :force => true do |t|
@@ -244,8 +244,8 @@ ActiveRecord::Schema.define(:version => 20120919013335) do
t.decimal "adjustment_total", :precision => 8, :scale => 2, :default => 0.0, :null => false
t.decimal "credit_total", :precision => 8, :scale => 2, :default => 0.0, :null => false
t.integer "user_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "completed_at"
t.integer "bill_address_id"
t.integer "ship_address_id"
@@ -266,8 +266,8 @@ ActiveRecord::Schema.define(:version => 20120919013335) do
t.text "description"
t.boolean "active", :default => true
t.string "environment", :default => "development"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "deleted_at"
t.string "display_on"
end
@@ -275,8 +275,8 @@ ActiveRecord::Schema.define(:version => 20120919013335) do
create_table "spree_payments", :force => true do |t|
t.decimal "amount", :precision => 8, :scale => 2, :default => 0.0, :null => false
t.integer "order_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.integer "source_id"
t.string "source_type"
t.integer "payment_method_id"
@@ -305,8 +305,8 @@ ActiveRecord::Schema.define(:version => 20120919013335) do
t.integer "owner_id"
t.string "owner_type"
t.text "value"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.string "key"
t.string "value_type"
end
@@ -331,16 +331,16 @@ ActiveRecord::Schema.define(:version => 20120919013335) do
t.integer "position"
t.integer "product_id"
t.integer "option_type_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "spree_product_properties", :force => true do |t|
t.string "value"
t.integer "product_id"
t.integer "property_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "spree_product_properties", ["product_id"], :name => "index_product_properties_on_product_id"
@@ -364,8 +364,8 @@ ActiveRecord::Schema.define(:version => 20120919013335) do
t.string "meta_keywords"
t.integer "tax_category_id"
t.integer "shipping_category_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.integer "count_on_hand", :default => 0, :null => false
t.integer "supplier_id"
t.boolean "group_buy"
@@ -409,8 +409,8 @@ ActiveRecord::Schema.define(:version => 20120919013335) do
t.integer "user_id"
t.integer "product_group_id"
t.string "type"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "spree_promotion_rules", ["product_group_id"], :name => "index_promotion_rules_on_product_group_id"
@@ -427,8 +427,8 @@ ActiveRecord::Schema.define(:version => 20120919013335) do
create_table "spree_properties", :force => true do |t|
t.string "name"
t.string "presentation", :null => false
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "spree_properties_prototypes", :id => false, :force => true do |t|
@@ -438,8 +438,8 @@ ActiveRecord::Schema.define(:version => 20120919013335) do
create_table "spree_prototypes", :force => true do |t|
t.string "name"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "spree_return_authorizations", :force => true do |t|
@@ -448,8 +448,8 @@ ActiveRecord::Schema.define(:version => 20120919013335) do
t.decimal "amount", :precision => 8, :scale => 2, :default => 0.0, :null => false
t.integer "order_id"
t.text "reason"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "spree_roles", :force => true do |t|
@@ -472,8 +472,8 @@ ActiveRecord::Schema.define(:version => 20120919013335) do
t.integer "order_id"
t.integer "shipping_method_id"
t.integer "address_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.string "state"
end
@@ -481,15 +481,15 @@ ActiveRecord::Schema.define(:version => 20120919013335) do
create_table "spree_shipping_categories", :force => true do |t|
t.string "name"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "spree_shipping_methods", :force => true do |t|
t.string "name"
t.integer "zone_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.string "display_on"
t.integer "shipping_category_id"
t.boolean "match_none"
@@ -505,8 +505,8 @@ ActiveRecord::Schema.define(:version => 20120919013335) do
t.integer "transaction_id"
t.integer "customer_id"
t.string "payment_type"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "spree_state_changes", :force => true do |t|
@@ -514,8 +514,8 @@ ActiveRecord::Schema.define(:version => 20120919013335) do
t.string "previous_state"
t.integer "stateful_id"
t.integer "user_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.string "stateful_type"
t.string "next_state"
end
@@ -529,8 +529,8 @@ ActiveRecord::Schema.define(:version => 20120919013335) do
create_table "spree_tax_categories", :force => true do |t|
t.string "name"
t.string "description"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.boolean "is_default", :default => false
t.datetime "deleted_at"
end
@@ -539,15 +539,15 @@ ActiveRecord::Schema.define(:version => 20120919013335) do
t.decimal "amount", :precision => 8, :scale => 5
t.integer "zone_id"
t.integer "tax_category_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.boolean "included_in_price", :default => false
end
create_table "spree_taxonomies", :force => true do |t|
t.string "name", :null => false
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "spree_taxons", :force => true do |t|
@@ -556,8 +556,8 @@ ActiveRecord::Schema.define(:version => 20120919013335) do
t.string "name", :null => false
t.string "permalink"
t.integer "taxonomy_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.integer "lft"
t.integer "rgt"
t.string "icon_file_name"
@@ -575,8 +575,8 @@ ActiveRecord::Schema.define(:version => 20120919013335) do
t.integer "permissable_id"
t.string "permissable_type"
t.string "token"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "spree_tokenized_permissions", ["permissable_id", "permissable_type"], :name => "index_tokenized_name_and_type"
@@ -585,8 +585,8 @@ ActiveRecord::Schema.define(:version => 20120919013335) do
t.string "environment"
t.string "analytics_id"
t.boolean "active", :default => true
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "spree_users", :force => true do |t|
@@ -607,8 +607,8 @@ ActiveRecord::Schema.define(:version => 20120919013335) do
t.string "login"
t.integer "ship_address_id"
t.integer "bill_address_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.string "authentication_token"
t.string "unlock_token"
t.datetime "locked_at"
@@ -631,7 +631,7 @@ ActiveRecord::Schema.define(:version => 20120919013335) do
t.boolean "is_master", :default => false
t.integer "product_id"
t.integer "count_on_hand", :default => 0, :null => false
t.decimal "cost_price", :precision => 8, :scale => 2
t.decimal "cost_price", :precision => 8, :scale => 2, :default => 0.0
t.integer "position"
end
@@ -641,15 +641,15 @@ ActiveRecord::Schema.define(:version => 20120919013335) do
t.integer "zoneable_id"
t.string "zoneable_type"
t.integer "zone_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "spree_zones", :force => true do |t|
t.string "name"
t.string "description"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.boolean "default_tax", :default => false
t.integer "zone_members_count", :default => 0
end
@@ -660,8 +660,8 @@ ActiveRecord::Schema.define(:version => 20120919013335) do
t.string "email"
t.string "twitter"
t.string "website"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.integer "address_id"
end

View File

@@ -35,6 +35,7 @@ describe Spree::HomeController do
controller.stub(:current_user).and_return(user)
controller.stub(:current_order).and_return(current_order)
incomplete_order.should_receive(:destroy)
incomplete_order.should_receive(:merge!).never
current_order.should_receive(:merge!).never