Skip to content
This repository
Browse code

Fix issue where order was not being remembered

Fixes #1066
  • Loading branch information...
commit f440558f205ecc146fa85a1b6f14885efc48c72e 1 parent cfc4a34
Ryan Bigg authored May 31, 2012
7  core/app/models/spree/order.rb
@@ -464,6 +464,13 @@ def insufficient_stock_lines
464 464
       line_items.select &:insufficient_stock?
465 465
     end
466 466
 
  467
+    def merge!(order)
  468
+      order.line_items.each do |line_item|
  469
+        self.add_variant(line_item.variant, line_item.quantity)
  470
+      end
  471
+      order.destroy
  472
+    end
  473
+
467 474
     private
468 475
       def link_by_email
469 476
         self.email = user.email if self.user and not user.anonymous?
4  core/config/initializers/user_class_extensions.rb
@@ -13,6 +13,10 @@
13 13
       def has_spree_role?(role_in_question)
14 14
         spree_roles.any? { |role| role.name == role_in_question.to_s }
15 15
       end
  16
+
  17
+      def last_incomplete_spree_order
  18
+        spree_orders.incomplete.order("created_at desc").last
  19
+      end
16 20
     end
17 21
   end
18 22
 end
13  core/lib/spree/core/controller_helpers.rb
@@ -5,6 +5,7 @@ module ControllerHelpers
5 5
       def self.included(receiver)
6 6
         receiver.send :layout, :get_layout
7 7
         receiver.send :before_filter, 'set_user_language'
  8
+        receiver.send :before_filter, 'set_current_order'
8 9
 
9 10
         receiver.send :helper_method, 'title'
10 11
         receiver.send :helper_method, 'title='
@@ -46,8 +47,20 @@ def spree_current_user
46 47
         nil
47 48
       end
48 49
 
  50
+
49 51
       protected
50 52
 
  53
+      def set_current_order
  54
+        if spree_current_user
  55
+          last_incomplete_order = spree_current_user.last_incomplete_spree_order
  56
+          if session[:order_id].nil?
  57
+            session[:order_id] = last_incomplete_order.id
  58
+          elsif current_order && last_incomplete_order && current_order != last_incomplete_order
  59
+            current_order.merge!(last_incomplete_order)
  60
+          end
  61
+        end
  62
+      end
  63
+
51 64
       # Needs to be overriden so that we use Spree's Ability rather than anyone else's.
52 65
       def current_ability
53 66
         @current_ability ||= Spree::Ability.new(spree_current_user)

0 notes on commit f440558

Please sign in to comment.
Something went wrong with that request. Please try again.