mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-04 22:16:08 +00:00
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.
38 lines
932 B
Ruby
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
|