Class: Spree::Catalogs::ResolveQuantityRules

Inherits:
Object
  • Object
show all
Defined in:
app/services/spree/catalogs/resolve_quantity_rules.rb

Overview

Answers "how may this buyer order this variant" by walking their catalogs nearest-agreement-first.

Resolution is per field, not all-or-nothing: the first catalog that states a minimum wins the minimum, the first that states a multiple wins the multiple, and an agreement silent on a field passes it through to the next rather than waiving it — a nearer catalog with no stated minimum must not silently drop the one behind it. Within a single catalog the variant override row beats that catalog's own default columns. Nothing anywhere leaves the variant's own base rules.

The catalogs walked are whatever Spree::Catalog.for_buyer resolved, and that chain is exclusive by tier: a buyer with company catalogs never also carries their customer group's or the channel's default. So a channel-wide term is the floor for buyers who resolve NO company or group catalog, not a fallback beneath one — a company agreement that states nothing about a field lands on the variant's base rule, not on the channel default.

Built once per cart or request and reused: resolving a fifty-line cart must not be fifty passes over the same catalogs.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(catalogs) ⇒ ResolveQuantityRules

Returns a new instance of ResolveQuantityRules.

Parameters:



26
27
28
# File 'app/services/spree/catalogs/resolve_quantity_rules.rb', line 26

def initialize(catalogs)
  @catalogs = Array(catalogs)
end

Class Method Details

.for_purchase(purchase) ⇒ Spree::Catalogs::ResolveQuantityRules

Builds a resolver for a purchase's buyer, reusing the request's already-resolved catalog set where the purchase belongs to the store being served.



36
37
38
# File 'app/services/spree/catalogs/resolve_quantity_rules.rb', line 36

def self.for_purchase(purchase)
  new(catalogs_for(purchase))
end

Instance Method Details

#call(variant) ⇒ Spree::QuantityRule

Returns the buyer's effective rules.

Parameters:

Returns:



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/services/spree/catalogs/resolve_quantity_rules.rb', line 42

def call(variant)
  return variant.quantity_rule if catalogs.empty?

  minimum = nil
  multiple = nil

  catalogs.each do |catalog|
    override = override_for(catalog, variant)

    minimum ||= override&.minimum_order_quantity || catalog.minimum_order_quantity
    multiple ||= override&.order_multiple || catalog.order_multiple

    break if minimum && multiple
  end

  Spree::QuantityRule.new(
    minimum_order_quantity: minimum || variant.minimum_order_quantity,
    order_multiple: multiple || variant.order_multiple
  )
end

#order_minimum(currency) ⇒ Spree::CatalogOrderMinimum?

The order minimum in effect for a currency: the first catalog with a row for it. Catalogs saying nothing about this currency pass through, so an agreement priced only in EUR does not waive a USD threshold set by another catalog in the same tier.

Parameters:

  • currency (String)

Returns:



70
71
72
73
74
75
76
77
78
79
# File 'app/services/spree/catalogs/resolve_quantity_rules.rb', line 70

def order_minimum(currency)
  return nil if currency.blank? || catalogs.empty?

  code = currency.to_s.upcase
  catalogs.each do |catalog|
    row = minimums_for(catalog)[code]
    return row if row
  end
  nil
end