load exclusively the shipping methods that support all the shipping categories of the line items

This commit is contained in:
Mohamed ABDELLANI
2023-02-27 19:26:24 +01:00
parent 890ab6796e
commit eab8e2be6c
3 changed files with 25 additions and 4 deletions

View File

@@ -15,7 +15,10 @@ module CheckoutCallbacks
prepend_before_action :require_distributor_chosen
before_action :load_order, :associate_user, :load_saved_addresses, :load_saved_credit_cards
before_action :load_shipping_methods, if: -> { params[:step] == "details" }
before_action :load_shipping_methods,
:load_allowed_shipping_methods, if: -> {
params[:step] == "details"
}
before_action :ensure_order_not_completed
before_action :ensure_checkout_allowed
@@ -47,7 +50,25 @@ module CheckoutCallbacks
end
def load_shipping_methods
@shipping_methods = available_shipping_methods.sort { |a, b| a.name.casecmp(b.name) }
@shipping_methods = sorted_available_shipping_methods
end
def load_allowed_shipping_methods
@allowed_shipping_methods = sorted_available_shipping_methods.filter(
&method(:supports_all_products_shipping_categories?)
)
end
def sorted_available_shipping_methods
available_shipping_methods.sort { |a, b| a.name.casecmp(b.name) }
end
def supports_all_products_shipping_categories?(shipping_method)
(products_shipping_categories - shipping_method.shipping_categories.pluck(:id)).empty?
end
def products_shipping_categories
@products_shipping_categories ||= @order.products.pluck(:shipping_category_id).uniq
end
def redirect_to_shop?

View File

@@ -24,7 +24,7 @@ class SplitCheckoutController < ::BaseController
check_step if params[:step]
recalculate_tax if params[:step] == "summary"
flash_error_when_no_shipping_method_available if available_shipping_methods.none?
flash_error_when_no_shipping_method_available if load_allowed_shipping_methods.none?
end
def update

View File

@@ -77,7 +77,7 @@
- ship_method_description = nil
- selected_shipping_method ||= @shipping_methods[0].id if @shipping_methods.length == 1
- @shipping_methods.each do |shipping_method|
- @allowed_shipping_methods.each do |shipping_method|
- ship_method_is_selected = shipping_method.id == selected_shipping_method.to_i
%div.checkout-input.checkout-input-radio
= fields_for shipping_method do |shipping_method_form|