Created
December 6, 2016 06:29
-
-
Save wikimo/c41670e90f35afe65f541ff6018ed193 to your computer and use it in GitHub Desktop.
Revisions
-
wikimo created this gist
Dec 6, 2016 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,34 @@ require_dependency 'spree/calculator' module Spree class Calculator::FlatWithProducts < Calculator preference :amount, :decimal, default: 0 preference :currency, :string, default: ->{ Spree::Config[:currency] } def self.description Spree.t(:flat_with_products) end def compute(object=nil) if object && preferred_currency.upcase == object.currency.upcase # get promotion and related products promotion = Spree::Promotion.find_by_description '1A/1B' promotion_products = promotion.products # check line_items if exist in promotion product_nums = [] promotion_products.each do |check_product| object.line_items.each do |item| if item.variant_id == check_product.id product_nums << item.quantity end end end return product_nums.min * preferred_amount if product_nums.size > 0 end 0 end end end