Ruby 使用Shopify脚本编辑器按标签设置静态价格
我已经研究过类似的问题和解决方案,但我无法让它们与我的问题和解决方案协同工作。我需要有一个50美元的静态价格设置为一系列产品,没有具体的折扣我可以申请,因为这些产品的实际价格都有所不同。以下是我目前掌握的代码:Ruby 使用Shopify脚本编辑器按标签设置静态价格,ruby,shopify,Ruby,Shopify,我已经研究过类似的问题和解决方案,但我无法让它们与我的问题和解决方案协同工作。我需要有一个50美元的静态价格设置为一系列产品,没有具体的折扣我可以申请,因为这些产品的实际价格都有所不同。以下是我目前掌握的代码: class StaticPrice def initialize(selector) @selector = selector end 标记选择器 class TagSelector def initialize(tag) @tag =
class StaticPrice
def initialize(selector)
@selector = selector
end
标记选择器
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
当您实例化一个新的StaticPrice对象时,您将发送3个属性,但您的对象只接受其中一个。您实例化了TagSelector,但从未使用匹配项?方法 如果不了解更多信息、看到更多信息,也没有更多的解释,那么您提供的代码量就没有多大用处
为什么不迭代购物车,然后设定价格呢。这很简单,不涉及复杂的对象。尝试使用更简单的代码,并在获得信心的同时建立一种更有组织的方法。几乎不需要campaign、StaticPrice和TagSelector,直到您实际有了一些显示需要它们的工作脚本代码。当您实例化一个新的StaticPrice对象时,您发送了3个属性,但您的对象只接受其中一个。您实例化了TagSelector,但从未使用匹配项?方法 如果不了解更多信息、看到更多信息,也没有更多的解释,那么您提供的代码量就没有多大用处
为什么不迭代购物车,然后设定价格呢。这很简单,不涉及复杂的对象。尝试使用更简单的代码,并在获得信心的同时建立一种更有组织的方法。在您实际拥有一些工作脚本代码来展示对它们的需求之前,几乎不需要活动、StaticPrice和TagSelector。非常感谢您的建议,恐怕我不太擅长这一点,而且我很难在周一之前完成这项工作,而且我找不到任何人为我写这项工作,所以我正试图根据shopify中预先存在的脚本(不多)和我在GitHub上找到的一些东西,快速将弗兰肯斯坦的东西整合起来。。如果你或任何人有一个简单的方法来做到这一点,我将永远感激,对不起,我需要帮助,我正在努力提高我的技能,以便能够真正提供帮助,而不仅仅是寻求帮助。嘿,伙计们,我成功地使它发挥作用,我在这里发布我的结果主要是为了缓解喜剧情绪,我敢肯定这是一个臃肿的代码,有大量不必要的东西在进行,但我让它工作。基本上,这将根据商品的标签改变购物车中商品的价格,并限制任何人将优惠券与价格相结合。非常感谢您的建议,恐怕我不太擅长这一点,而且我很难在周一之前完成这项工作,而且我找不到任何人为我写这项工作,所以我正试图根据shopify中预先存在的脚本(不多)和我在GitHub上找到的一些东西,快速将弗兰肯斯坦的东西整合起来。。如果你或任何人有一个简单的方法来做到这一点,我将永远感激,对不起,我需要帮助,我正在努力提高我的技能,以便能够真正提供帮助,而不仅仅是寻求帮助。嘿,伙计们,我成功地使它发挥作用,我在这里发布我的结果主要是为了缓解喜剧情绪,我敢肯定这是一个臃肿的代码,有大量不必要的东西在进行,但我让它工作。基本上,这将根据商品的标签更改购物车中商品的价格,并限制任何人将优惠券与价格相结合。不确定这是否是导致错误的原因,但在格式化代码时,您的
StaticPrice
类缺少一个end
,不确定这是否是导致错误的原因,但在格式化代码时,您的StaticPrice
类缺少end