diff --git a/app/assets/javascripts/store/controllers/cart.js.coffee b/app/assets/javascripts/store/controllers/cart.js.coffee index 240cb791f4..319418d548 100644 --- a/app/assets/javascripts/store/controllers/cart.js.coffee +++ b/app/assets/javascripts/store/controllers/cart.js.coffee @@ -3,8 +3,14 @@ angular.module('store', ['ngResource']). controller 'CartCtrl', ($scope, $window, CartFactory) -> - $scope.loadCart = -> - $scope.cart = CartFactory.load(1) + $scope.state = 'Empty' + + $scope.loadCart = (cart_id) -> + if cart_id? + CartFactory.load cart_id, (cart) -> + $scope.cart = cart + if $scope.cart?.orders?.length > 0 + $scope.state = "There's something there...." $scope.addVariant = (variant, quantity) -> diff --git a/app/controllers/spree/orders_controller_decorator.rb b/app/controllers/spree/orders_controller_decorator.rb index 10788004a3..b6be15431d 100644 --- a/app/controllers/spree/orders_controller_decorator.rb +++ b/app/controllers/spree/orders_controller_decorator.rb @@ -6,6 +6,10 @@ Spree::OrdersController.class_eval do # Patch Orders#populate to provide distributor_id and order_cycle_id to OrderPopulator def populate + if OpenFoodWeb::FeatureToggle.enabled? :multi_cart + populate_cart params[:variants] + end + populator = Spree::OrderPopulator.new(current_order(true), current_currency) if populator.populate(params.slice(:products, :variants, :quantity, :distributor_id, :order_cycle_id)) fire_event('spree.cart.add') @@ -76,4 +80,19 @@ Spree::OrdersController.class_eval do end end end + + def populate_cart variants + if spree_current_user + unless spree_current_user.cart + spree_current_user.build_cart + cart = Cart.create(user: spree_current_user) + spree_current_user.cart = cart + spree_current_user.save + end + variants.each do |variant_id, quantity| + variant = Spree::Variant.find(variant_id) + spree_current_user.cart.add_variant variant, quantity + end + end + end end diff --git a/app/models/spree/user_decorator.rb b/app/models/spree/user_decorator.rb index bbd4c30177..5acaff924e 100644 --- a/app/models/spree/user_decorator.rb +++ b/app/models/spree/user_decorator.rb @@ -1,6 +1,7 @@ Spree.user_class.class_eval do has_many :enterprise_roles, :dependent => :destroy has_many :enterprises, through: :enterprise_roles + has_one :cart accepts_nested_attributes_for :enterprise_roles, :allow_destroy => true diff --git a/app/views/open_food_web/cart/_show.html.haml b/app/views/open_food_web/cart/_show.html.haml index 7ca89c6633..73f136b9db 100644 --- a/app/views/open_food_web/cart/_show.html.haml +++ b/app/views/open_food_web/cart/_show.html.haml @@ -1,8 +1,8 @@ / %script = Spree.api_key = raw(try_spree_current_user.try(:spree_api_key).to_s.inspect) Hello -%div{ 'ng-app' => 'store', 'ng-controller' => 'CartCtrl', 'ng-init' => "loadCart();" } - {{cart}} +%div{ 'ng-app' => 'store', 'ng-controller' => 'CartCtrl', 'ng-init' => "loadCart(#{try_spree_current_user.andand.cart.andand.id});" } + {{cart}} {{state}} %ul %li(ng-repeat="order in cart.orders") {{order.distributor}}