I need to select affordable taxa and their children.
I use this custom rule:
module Spree
class Promotion
module Rules
class TaxonPromotionRule < Spree::PromotionRule
has_and_belongs_to_many :taxon, class_name: '::Spree::Taxon', join_table: 'spree_taxons_promotion_rules', foreign_key: 'promotion_rule_id'
validate :only_one_promotion_per_product
MATCH_POLICIES = %w(any all)
preference :match_policy, :string, default: MATCH_POLICIES.first
def eligible_taxons
taxon
end
def applicable?(promotable)
promotable.is_a?(Spree::Order)
end
def eligible?(order, options = {})
return false if eligible_taxons.empty?
if preferred_match_policy == 'all'
eligible_taxons.all? {|p| order.products.include_taxon?(p) }
else
order.products.any? {|p| eligible_taxons.any? {|t| t.include_product?(p)} }
end
end
def taxon_ids_string
taxon_ids.join(',')
end
def taxon_ids_string=(s)
self.taxon_ids = s.to_s.split(',').map(&:strip)
end
private
def only_one_promotion_per_product
if Spree::Promotion::Rules::TaxonPromotionRule.all.map(&:taxon).flatten.uniq!
errors[:base] << "You can't create two promotions for the same product"
end
end
end
end
end
end
and decorator:
Spree::Taxon.class_eval do
def include_product? p
products.include? p
end
end
I want eligible_taxons to be a taxon from the rules table and the entire child identifier. Therefore, if I set some root category, this rule will apply to all child categories. Hope my question is clear and clear. :)
source
share