skip to Main Content

I am new to shopify. Now I want to use shopify API to create a price rule, this is my source code here
I use 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

However it always return me false. If anyone has the idea? Thanks a million!

2

Answers


  1. Please check this Api to create price rule(Shopify Api). I have used this Api in php and its working fine for me.

    I have created App and then use Api key and secret key to generate price rule.

    Thanks

    Login or Signup to reply.
  2. For the ones coming to this question, one could fetch Price rules as per shopify_app gem for Rails applications as:

    First allow your app to access read/write permissions in initializers/shopify.rb file as:

    config.scope = "read_products, write_products, read_price_rules, write_price_rules"
    

    After that you can fetch price rules as:

    @price_rules = ShopifyAPI::PriceRule.find(:all, params:{id: '4171201931'})
    

    And can also create a price rule as:

        @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
    

    There are validations involved. Incase you want to check the response, one may inspect it like @create_price_rule.inspect

    Or even you can delete a PriceRule as:

    @price_rules = ShopifyAPI::PriceRule.find(:all).first
    @price_rules.destroy
    @last_price_rule = ShopifyAPI::PriceRule.find(4171860875)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search