diff --git a/app/helpers/add_to_cart_helper.rb b/app/helpers/add_to_cart_helper.rb new file mode 100644 index 0000000000..1e41219d4e --- /dev/null +++ b/app/helpers/add_to_cart_helper.rb @@ -0,0 +1,19 @@ +module AddToCartHelper + def product_out_of_stock + !@product.has_stock? && !Spree::Config[:allow_backorders] + end + + def product_incompatible_with_current_order(order, product) + !order.nil? && !DistributorChangeValidator.new(order).can_change_distributor? && !Enterprise.distributing_product(product).include?(order.distributor) + end + + def available_distributors_for(order, product) + distributors = Enterprise.distributing_product(product) + + if order && order.distributor + distributors = DistributorChangeValidator.new(order).available_distributors(distributors) + end + + distributors + end +end diff --git a/app/views/spree/products/_add_to_cart.html.haml b/app/views/spree/products/_add_to_cart.html.haml index db937fce00..3ea1778819 100644 --- a/app/views/spree/products/_add_to_cart.html.haml +++ b/app/views/spree/products/_add_to_cart.html.haml @@ -1,37 +1,26 @@ .add-to-cart - order = current_order(false) - - if !@product.has_stock? && !Spree::Config[:allow_backorders] + + - if product_out_of_stock = content_tag('strong', t(:out_of_stock)) - - elsif !order.nil? && !DistributorChangeValidator.new(order).can_change_distributor? && !@product.distributors.include?(order.distributor) - .error-distributor - Please complete your order at - = link_to current_distributor.name, root_path - before shopping with another distributor. + - elsif product_incompatible_with_current_order(order, @product) + = render 'add_to_cart_distributor_unavailable' + - else - %div(class = "columns alpha two") - %div Quantity - = number_field_tag (@product.has_variants? ? :quantity : "variants[#{@product.master.id}]"), 1, :class => 'title', :in => 1..@product.on_hand - - if @product.group_buy - %div(class = "columns alpha three") - %div Max quantity - = number_field_tag (@product.has_variants? ? :max_quantity : "variant_attributes[#{@product.master.id}][max_quantity]"), 1, :class => 'title max_quantity', :in => 1..@product.on_hand + = render 'add_to_cart_quantity_fields', product: @product + %div.cleared %br - - if order.nil? || order.distributor.nil? - %div Distributor for your order: - = select_tag "distributor_id", options_from_collection_for_select([Enterprise.new]+@product.distributors, "id", "name", :include_blank => '') + - available_distributors = available_distributors_for(order, @product) + - if available_distributors.length > 1 || order.andand.distributor.nil? + = render 'add_to_cart_distributor_choice', distributor_collection: available_distributors + - else - - available_distributors = DistributorChangeValidator.new(order).available_distributors(@product.distributors) - - if available_distributors.length > 1 - %div Distributor for your order: - = select_tag "distributor_id", options_from_collection_for_select(available_distributors, "id", "name", current_distributor.andand.id) - - elsif !@product.distributors.include? order.distributor - = hidden_field_tag "distributor_id", available_distributors.first.id - .distributor-fixed= "Your distributor for this order will be changed to #{available_distributors.first.name} if you add this product to your cart." - - else - = hidden_field_tag "distributor_id", order.distributor.id - .distributor-fixed= "Your distributor for this order is #{order.distributor.name}" + - distributor = available_distributors.first + - changing_distributor = distributor != order.andand.distributor + = render 'add_to_cart_distributor_fixed', distributor: distributor, changing_distributor: changing_distributor + %br = button_tag :class => 'large primary', :id => 'add-to-cart-button', :type => :submit do = t(:add_to_cart) diff --git a/app/views/spree/products/_add_to_cart_distributor_choice.html.haml b/app/views/spree/products/_add_to_cart_distributor_choice.html.haml new file mode 100644 index 0000000000..2a1246e1ac --- /dev/null +++ b/app/views/spree/products/_add_to_cart_distributor_choice.html.haml @@ -0,0 +1,2 @@ +%div Distributor for your order: += select_tag "distributor_id", options_from_collection_for_select([Enterprise.new]+distributor_collection, "id", "name", current_distributor.andand.id) diff --git a/app/views/spree/products/_add_to_cart_distributor_fixed.html.haml b/app/views/spree/products/_add_to_cart_distributor_fixed.html.haml new file mode 100644 index 0000000000..7927f9fc3e --- /dev/null +++ b/app/views/spree/products/_add_to_cart_distributor_fixed.html.haml @@ -0,0 +1,6 @@ += hidden_field_tag "distributor_id", distributor.id + +- if changing_distributor + .distributor-fixed= "Your distributor for this order will be changed to #{distributor.name} if you add this product to your cart." +- else + .distributor-fixed= "Your distributor for this order is #{distributor.name}" diff --git a/app/views/spree/products/_add_to_cart_distributor_unavailable.html.haml b/app/views/spree/products/_add_to_cart_distributor_unavailable.html.haml new file mode 100644 index 0000000000..fb84bd1e9f --- /dev/null +++ b/app/views/spree/products/_add_to_cart_distributor_unavailable.html.haml @@ -0,0 +1,4 @@ +.error-distributor + Please complete your order at + = link_to current_distributor.name, root_path + before shopping with another distributor. diff --git a/app/views/spree/products/_add_to_cart_quantity_fields.html.haml b/app/views/spree/products/_add_to_cart_quantity_fields.html.haml new file mode 100644 index 0000000000..826464b162 --- /dev/null +++ b/app/views/spree/products/_add_to_cart_quantity_fields.html.haml @@ -0,0 +1,7 @@ +%div(class = "columns alpha two") + %div Quantity + = number_field_tag (product.has_variants? ? :quantity : "variants[#{product.master.id}]"), 1, :class => 'title', :in => 1..product.on_hand +- if product.group_buy + %div(class = "columns alpha three") + %div Max quantity + = number_field_tag (product.has_variants? ? :max_quantity : "variant_attributes[#{product.master.id}][max_quantity]"), 1, :class => 'title max_quantity', :in => 1..product.on_hand