Class: Spree::RelatedProductDiscount
- Inherits:
-
Calculator
- Object
- Calculator
- Spree::RelatedProductDiscount
- Defined in:
- app/models/spree/calculator/related_product_discount.rb
Class Method Summary collapse
Instance Method Summary collapse
Class Method Details
.description ⇒ Object
5 6 7 |
# File 'app/models/spree/calculator/related_product_discount.rb', line 5 def self.description I18n.t("related_product_discount") end |
.register ⇒ Object
9 10 11 12 |
# File 'app/models/spree/calculator/related_product_discount.rb', line 9 def self.register super Spree::Coupon.register_calculator(self) end |
Instance Method Details
#compute(object) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'app/models/spree/calculator/related_product_discount.rb', line 14 def compute(object) if object.is_a?(Array) return if object.empty? order = object.first.order else order = object end return unless eligible?(order) total = order.line_items.inject(0) do |total, line_item| relations = Spree::Relation.find(:all, :conditions => ["discount_amount <> 0.0 AND relatable_type = ? AND relatable_id = ?", "Product", line_item.variant.product.id]) discount_applies_to = relations.map {|rel| rel..master } order.line_items.each do |li| if discount_applies_to.include? li.variant discount = relations.detect {|rel| rel..variant == li.variant}.discount_amount total += if li.quantity < line_item.quantity (discount * li.quantity) else (discount * line_item.quantity) end end end total end total == 0 ? nil : total end |
#eligible?(order) ⇒ Boolean
45 46 47 |
# File 'app/models/spree/calculator/related_product_discount.rb', line 45 def eligible?(order) order.line_items.any? { |line_item| Spree::Relation.exists?(["discount_amount <> 0.0 AND relatable_type = ? AND relatable_id = ?", "Product", line_item.variant.product.id])} end |