Fix multi-cart association and add some temporary multi-cart support into the current front end.

This commit is contained in:
Andrew Spinks
2013-08-09 11:00:32 +10:00
parent 77d31b3004
commit 0a39fb87a5
4 changed files with 30 additions and 4 deletions

View File

@@ -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) ->

View File

@@ -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

View File

@@ -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

View File

@@ -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}}