Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
I have created a function to get all possible promotions of a product by considering even the taxons. Its not yet optimised and not well tested. I added the following in the product decorator model
def get_all_possible_promotions
taxons_of_product_including_parents
end
private
# ids of taxons rules and taxons rules children
def taxons_including_children_ids
promotion_rules_taxons = Spree::Taxon.where("id IN (?)", Spree::PromotionRuleTaxon.select(:taxon_id).where("True") )
promotion_rules_taxons.inject([]) { |ids, taxon| ids += taxon.self_and_descendants.ids }
end
# taxons order vs taxons rules and taxons rules children
def product_taxons_in_taxons_and_children()
self.taxons.where(id: taxons_including_children_ids)
end
def taxons_of_product_including_parents()
eligible_taxons = product_taxons_in_taxons_and_children().inject([]) { |taxons, taxon| taxons << taxon.self_and_ancestors }.flatten.uniq
final_taxon_promotions = eligible_taxons.inject([]) { |promotions,taxon|
promotions += taxon.promotion_rules.inject([]){|promotion,promotion_rule|
promotion << promotion_rule.promotion if ! promotion_rule.promotion.expired?
}
}.flatten.uniq
final_taxon_promotions += self.possible_promotions
final_taxon_promotions.uniq
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment