Solved

Price Rule creation via API or GraphQL seems like is fine but the discount stuck on "still loading"

ManuelLop
Shopify Partner
29 2 2

I tried for 3 days and the discount still loading. The status is "active" but doesn't works on checkout. Do I need Shopify Plus for creating a price rule from API or GraphQL? Is this "still loading" a Shopify Issue?

Accepted Solution (1)
ManuelLop
Shopify Partner
29 2 2

This is an accepted solution.

Thanks to @OliUK he actually found a solution and this is:

I've got this working now. I found a guide online where the author creates the price rule first, then creates the discount afterwards. I think my problem was either the API nested discount code doesn't work when on the same call that creates the price rule OR you just have to make the discount and price rule separately OR you just have to code the prefix options this way.
I've pasted the relevant code below. PD: I tested and works perfectly well.

@price_rule = ShopifyAPI::PriceRule.create(
      title: "CrumbDiscount",
      target_type: "line_item",
      target_selection: "all",
      allocation_method: "across",
      value_type: "percentage",
      value: "-20.0",
      customer_selection: "all",
      starts_at: Time.now.iso8601, 
      usage_limit: 1
    )

    #create a new discount code
    discount_code = ShopifyAPI::DiscountCode.new
    discount_code.prefix_options[:price_rule_id] = @price_rule.id
    discount_code.code = "20Off#{rand(10 ** 10)}"
    discount_code.save
    render json: discount_code

 

 

View solution in original post

Replies 4 (4)

Kevin_A
Shopify Staff
318 42 61

Hey @ManuelLop 

Can you pass us the x-request-id response header from your mutation/POST or provide a link to what you are seeing?

Kevin_A | Solutions Engineer @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

ManuelLop
Shopify Partner
29 2 2

This is my mutation/POST response:

{"data":{"priceRuleCreate":{"priceRule":{"id":"gid:\/\/shopify\/PriceRule\/xxxxxxxxxxxx"},"priceRuleUserErrors":[]}},"extensions":{"cost":{"requestedQueryCost":10,"actualQueryCost":10,"throttleStatus":{"maximumAvailable":1000.0,"currentlyAvailable":990,"restoreRate":50.0}}}}

ManuelLop
Shopify Partner
29 2 2

IMG_20210310_113112.jpg

IMG_20210310_113145.jpg

ManuelLop
Shopify Partner
29 2 2

This is an accepted solution.

Thanks to @OliUK he actually found a solution and this is:

I've got this working now. I found a guide online where the author creates the price rule first, then creates the discount afterwards. I think my problem was either the API nested discount code doesn't work when on the same call that creates the price rule OR you just have to make the discount and price rule separately OR you just have to code the prefix options this way.
I've pasted the relevant code below. PD: I tested and works perfectly well.

@price_rule = ShopifyAPI::PriceRule.create(
      title: "CrumbDiscount",
      target_type: "line_item",
      target_selection: "all",
      allocation_method: "across",
      value_type: "percentage",
      value: "-20.0",
      customer_selection: "all",
      starts_at: Time.now.iso8601, 
      usage_limit: 1
    )

    #create a new discount code
    discount_code = ShopifyAPI::DiscountCode.new
    discount_code.prefix_options[:price_rule_id] = @price_rule.id
    discount_code.code = "20Off#{rand(10 ** 10)}"
    discount_code.save
    render json: discount_code