Files
openfoodnetwork/app/services/order_available_shipping_methods.rb
Cillian O'Ruanaidh a53a3259a8 Connect DistributorShippingMethods to OrderCycles instead of Spree::ShippingMethods
Before if a shipping method was shared between multiple distributors it could only be disabled/enabled on that order cycle for all the distributors which have that shipping method e.g. you couldn't select that shipping method for one distributor but disable it for another.
2022-09-30 13:13:39 +01:00

38 lines
932 B
Ruby

# frozen_string_literal: true
class OrderAvailableShippingMethods
attr_reader :order, :customer
delegate :distributor,
:order_cycle,
to: :order
def initialize(order, customer = nil)
@order, @customer = order, customer
end
def to_a
return [] if distributor.blank?
shipping_methods = shipping_methods_before_tag_rules_applied
applicator = OpenFoodNetwork::TagRuleApplicator.new(distributor,
"FilterShippingMethods", customer&.tag_list)
applicator.filter!(shipping_methods)
shipping_methods.uniq
end
private
def shipping_methods_before_tag_rules_applied
if order_cycle.nil? || order_cycle.simple?
distributor.shipping_methods
else
distributor.shipping_methods.where(
id: order_cycle.distributor_shipping_methods.select(:shipping_method_id)
)
end.frontend.to_a
end
end