Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shazadmaved/8d735d3e3e7bc5c98363670504d6f7c8 to your computer and use it in GitHub Desktop.
Save shazadmaved/8d735d3e3e7bc5c98363670504d6f7c8 to your computer and use it in GitHub Desktop.
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