Skip to content

Instantly share code, notes, and snippets.

@wikimo
Created December 6, 2016 06:29
Show Gist options
  • Save wikimo/c41670e90f35afe65f541ff6018ed193 to your computer and use it in GitHub Desktop.
Save wikimo/c41670e90f35afe65f541ff6018ed193 to your computer and use it in GitHub Desktop.

Revisions

  1. wikimo created this gist Dec 6, 2016.
    34 changes: 34 additions & 0 deletions flat_with_products.rb
    Original 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