我是新来店铺。现在我想使用shopify API创建一个价格规则,这是我的源代码 我使用rails 5.1.4,shopify_app 8.1.0
shop_url = "https://api_key:secret@domain/admin"
ShopifyAPI::Base.site = shop_url
prerequisite_saved_search_ids = [53677883419]
price_rule = ShopifyAPI::PriceRule.new
price_rule.title = "demodemo"
price_rule.target_name = "line_item"
price_rule.target_selection = "all"
price_rule.allocation_method = "across"
price_rule.value_type = "fixed_amount"
price_rule.value = "-10.0"
price_rule.customer_selection = "prerequisite"
price_rule.prerequisite_saved_search_ids = prerequisite_saved_search_ids
price_rule.start_at = Time.now.iso8601
res = price_rule.save
puts res
然而它总是让我失意。如果有人有这个想法?万分感谢!
答案 0 :(得分:0)
答案 1 :(得分:0)
对于那些提出这个问题的人,可以根据shopify_app gem获取Rails应用程序的Price规则:
首先允许您的应用在initializers / shopify.rb文件中访问读/写权限:
config.scope = "read_products, write_products, read_price_rules, write_price_rules"
之后,您可以将价格规则提取为:
@price_rules = ShopifyAPI::PriceRule.find(:all, params:{id: '4171201931'})
并且还可以创建价格规则:
@create_price_rule = ShopifyAPI::PriceRule.new(
price_rule: {
title: "FREESHIPPING2",
target_type: "shipping_line",
target_selection: "all",
allocation_method: "each",
value_type: "percentage",
value: "-100.0",
usage_limit: 20,
customer_selection: "all",
prerequisite_subtotal_range: {
greater_than_or_equal_to: "50.0"
},
starts_at: "2017-11-19T17:59:10Z"
}
)
@create_price_rule.save
涉及验证。如果您想检查响应,可以像@create_price_rule.inspect
甚至您可以将PriceRule删除为:
@price_rules = ShopifyAPI::PriceRule.find(:all).first
@price_rules.destroy
@last_price_rule = ShopifyAPI::PriceRule.find(4171860875)