使用Shopify脚本编辑器按标签设置静态价格

时间:2017-01-17 20:00:24

标签: ruby shopify

我已经检查了类似的问题和解决方案,但我无法让他们与我合作。我需要将一堆产品设置为50美元的静态价格,我可以申请没有具体折扣,因为这些产品的实际价格各不相同。这是我到目前为止的代码:

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

****更新......好吧,我得到了它的工作,但它非常臃肿,我很确定不专业(菜鸟在这里),但..它工作..这允许我设置产品的静态价格基于特定销售的标签,同时不允许某人使用优惠券从销售项目中获得任何额外价格..我感谢任何改进建议****

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

1 个答案:

答案 0 :(得分:0)

当您实例化一个新的StaticPrice对象时,您将发送3个属性,但您的对象只接受该属性。你实例化TagSelector,但你从不使用匹配?方法

在不了解更多信息,了解更多信息以及提供更多解释的情况下,您提供的代码数量几乎没用。

为什么不迭代购物车,并设定价格。这是微不足道的,不涉及复杂的对象。尝试使用更简单的代码,并在获得信心时构建更有条理的方法。在您实际拥有一些需要它们的工作脚本代码之前,几乎不需要使用StaticPrice和TagSelector。