[英]Use Shopify Script Editor to Set Static Price by Tag
I've examined similar questions and solutions but I was not able to get them to work with mine. 我已经研究了类似的问题和解决方案,但我无法让他们与我合作。 I need to have a bunch of products set to a static price of $50, there is no specific discount I can apply as the actual price on these all vary.
我需要将一堆产品设置为50美元的静态价格,我可以申请没有具体折扣,因为这些产品的实际价格各不相同。 Here is the code I have so far:
这是我到目前为止的代码:
class StaticPrice
def initialize(selector)
@selector = selector
end
TagSelector 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... Well I got it to work, however it's extremely bloated and I'm quite sure unprofessional (rookie here), but.. it works.. This allows me to set static prices on products based off tags for a particular sale while at the same time not allowing someone to use a coupon to get any additional price off of the sale item.. I appreciate any suggestions for improvement **** **** 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
When you are instantiating a new StaticPrice object, you are sending in 3 attributes but your object accepts only the one. 在实例化新的StaticPrice对象时,您将发送3个属性,但您的对象只接受该属性。 You instantiate TagSelector, but you never use the match?
你实例化TagSelector,但你从不使用匹配? method.
方法。
Without knowing more, seeing more, and having more of your explanations, the amount of code you are providing is of little use. 如果不了解更多,看到更多,并且有更多解释,那么您提供的代码量就没那么大了。
Why not just iterate the cart, and set prices. 为什么不迭代购物车,并设定价格。 That is trivial and does not involve complex objects.
这是微不足道的,不涉及复杂的对象。 Experiment with simpler code, and build up to a more organized approach as you gain confidence.
尝试使用更简单的代码,并在获得信心时构建更有条理的方法。 There is little need for the campaign, StaticPrice and TagSelector till you actually have some working script code that exhibits a need for them.
在您实际拥有一些显示需要它们的工作脚本代码之前,几乎不需要使用StaticPrice和TagSelector。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.