2017-01-17 10 views
1

私は同様の質問と解決策を検討しましたが、私はそれらを私の仕事で使用することができませんでした。私は$ 50の静的な価格に設定された製品の束を持っている必要があります、私はこれらのすべての実際の価格が異なるとして適用することができます特定の割引はありません。ここでのコードは、私がこれまで持っている:Shopify Script Editorを使用して静的価格をタグで設定する

class StaticPrice 

    def initialize(selector) 
    @selector = selector 
    end 

TagSelector

class TagSelector 

     def initialize(tag) 
     @tag = tag 
     end 

     def match?(line_item) 
     line_item.variant.product.tags.include?(@tag) 
     end 

    end 

CAMPAIGNS = [ 
    StaticPrice.new(
    TagSelector.new("boots"), 
    line_item.line_price == (5000), message: "SALE!") 
] 


Output.cart = Input.cart 

**** UPDATE ...私はそれが動作するようになったまあ、しかし、それは非常に肥大化だと私は専門外はかなり確信しています(ルーキーはここにある)が、それは働く..これは、特定の販売のタグに基づいて製品に静的な価格を設定すると同時に、誰かがクーポンを使用して販売アイテム..改善のための提案をいただきありがとうございます****

case Input.cart.discount_code 
when CartDiscount::Percentage 
      if Line_items.quantity > 1 
      Input.cart.discount_code.reject(message: "Coupons can not be combined with BOGO promotion") 
      end 
     end 

class ItemCampaign 
    def initialize(selector, discount, partitioner) 
    @selector = selector 
    @discount = discount 
    @partitioner = partitioner 
    end 
    def run(cart) 
    applicable_items = cart.line_items.select do |line_item| 
     @selector.match?(line_item) 
    end 
    discounted_items = @partitioner.partition(cart, applicable_items) 

    discounted_items.each do |line_item| 
     @discount.apply(line_item) 
    end 
    end 
end 
class TagSelector 
    def initialize(tag) 
    @tag = tag 
    end 
    def match?(line_item) 
    line_item.variant.product.tags.include?(@tag) 
    end 
end 
class PercentageDiscount50 
    def initialize(percent, message) 
    @percent = Money.new(cents: 100) * 50 
    @message = message 
    end 
    def apply(line_item) 
    line_discount = line_item.line_price - line_item.line_price + Money.new(cents: 100) * 50 
    new_line_price = Money.new(cents: 100) * 50 
    line_item.change_line_price(new_line_price, message: @message) 
    puts "Discounted line item with variant #{line_item.variant.id} by #{line_discount}." 
    end 
end 
class PercentageDiscount40 
    def initialize(percent, message) 
    @percent = Money.new(cents: 100) * 40 
    @message = message 
    end 
    def apply(line_item) 
    line_discount = line_item.line_price - line_item.line_price + Money.new(cents: 100) * 40 
    new_line_price = Money.new(cents: 100) * 40 
    line_item.change_line_price(new_line_price, message: @message) 
    puts "Discounted line item with variant #{line_item.variant.id} by #{line_discount}." 
    end 
end 
class PercentageDiscount30 
    def initialize(percent, message) 
    @percent = Money.new(cents: 100) * 30 
    @message = message 
    end 
    def apply(line_item) 
    line_discount = line_item.line_price - line_item.line_price + Money.new(cents: 100) * 30 
    new_line_price = Money.new(cents: 100) * 30 
    line_item.change_line_price(new_line_price, message: @message) 
    puts "Discounted line item with variant #{line_item.variant.id} by #{line_discount}." 
    end 
end 
class PercentageDiscount20 
    def initialize(percent, message) 
    @percent = Money.new(cents: 100) * 20 
    @message = message 
    end 
    def apply(line_item) 
    line_discount = line_item.line_price - line_item.line_price + Money.new(cents: 100) * 20 
    new_line_price = Money.new(cents: 100) * 20 
    line_item.change_line_price(new_line_price, message: @message) 
    puts "Discounted line item with variant #{line_item.variant.id} by #{line_discount}." 
    end 
end 
class PercentageDiscount10 
    def initialize(percent, message) 
    @percent = Money.new(cents: 100) * 10 
    @message = message 
    end 
    def apply(line_item) 
    line_discount = line_item.line_price - line_item.line_price + Money.new(cents: 100) * 10 
    new_line_price = Money.new(cents: 100) * 10 
    line_item.change_line_price(new_line_price, message: @message) 
    puts "Discounted line item with variant #{line_item.variant.id} by #{line_discount}." 
    end 
end 
class LowToHighPartitioner 
    def initialize(paid_item_count, discounted_item_count) 
    @paid_item_count = paid_item_count 
    @discounted_item_count = discounted_item_count 
    end 
    def partition(cart, applicable_line_items) 
    sorted_items = applicable_line_items.sort_by{|line_item| line_item.variant.price} 
    total_applicable_quantity = sorted_items.map(&:quantity).reduce(0, :+) 
    discounted_items_remaining = Integer(total_applicable_quantity/(@paid_item_count + @discounted_item_count) * @discounted_item_count) 
    discounted_items = [] 
    sorted_items.each do |line_item| 
     break if discounted_items_remaining == 0 
     discounted_item = line_item 
     if line_item.quantity > discounted_items_remaining 
     discounted_item = line_item.split(take: discounted_items_remaining) 
     position = cart.line_items.find_index(line_item) 
     cart.line_items.insert(position + 0, discounted_item) 
     end 
     discounted_items_remaining -= discounted_item.quantity 
     discounted_items.push(discounted_item) 
    end 
    discounted_items 
    end 
end 
CAMPAIGNS = [ 
    ItemCampaign.new(
    TagSelector.new("SCRIPT50"), 
    PercentageDiscount50.new(10, "$50 FINAL SALE!"), 
    LowToHighPartitioner.new(0,1), 
), 
    ItemCampaign.new(
    TagSelector.new("SCRIPT40"), 
    PercentageDiscount40.new(10, "$40 FINAL SALE!"), 
    LowToHighPartitioner.new(0,1), 
), 
    ItemCampaign.new(
    TagSelector.new("SCRIPT30"), 
    PercentageDiscount30.new(10, "$30 FINAL SALE!"), 
    LowToHighPartitioner.new(0,1), 
), 
    ItemCampaign.new(
    TagSelector.new("SCRIPT20"), 
    PercentageDiscount20.new(10, "$20 FINAL SALE!"), 
    LowToHighPartitioner.new(0,1), 
), 
    ItemCampaign.new(
    TagSelector.new("SCRIPT10"), 
    PercentageDiscount10.new(10, "$10 FINAL SALE!"), 
    LowToHighPartitioner.new(0,1), 
) 
] 
CAMPAIGNS.each do |campaign| 
    campaign.run(Input.cart) 
end 
Output.cart = Input.cart 
+0

あなたのエラーの原因となっているのかどうかわかりませんが、あなたのコードをフォーマットすると 'StaticPrice'クラスの' end'が見つかりません –

答えて

0

新しいStaticPriceオブジェクトをインスタンス化するときは、3つの属性を送信していますが、オブジェクトはその3つの属性のみを受け入れます。あなたはTagSelectorをインスタンス化しますが、あなたはそのマッチを使用しませんか?方法。

もっと多くのことを理解し、説明を多くすることなく、提供しているコードの量はほとんど役に立たない。

なぜカートを繰り返し、価格を設定するだけではないのですか?それは些細なことであり、複雑なオブジェクトには関係しません。よりシンプルなコードを試し、自信を持ってより組織的なアプローチを構築してください。キャンペーン、StaticPrice、TagSelectorは、実際にスクリプトコードが必要になるまではほとんど必要ありません。

+0

あなたのアドバイスをありがとうございます、私はあまり良くはありませんこれと私は月曜日までにこの仕事を働かせるために壁に向かっているし、誰かが私のためにそれを書くのを見つけることができないので、私はフランケンシュタインに既存のスクリプト(多くではない)私がGitHubで見つけたもののうち、いくつかのものがあります。 あなたや誰かがこれを行う簡単な方法を持っていれば、私は永遠に感謝して、貧しい人のために申し訳なく思っています。助けを求めるのではなく、実際に援助すること。 – Casey

+0

ねえ、私はうまく動くように管理しました。私は結果を漫画の救済のために主に掲示しています。これは膨大な量の不必要なものが残っている膨大なコードです。基本的にこれはカートの商品の価格を販売のタグに基づいて変更し、クーポンを価格と組み合わせることを制限します。 – Casey

関連する問題