Price Rules not working as expected

New Member
1 0 0

Our app has successfully been creating discount codes for months, however, we started getting 422 responses last Friday.


Here's data we are posting to "/admin/price_rules.json" in JS object format (converted to JSON before posting)


allocation_method: "each",
customer_selection: "all",
once_per_customer: true,
target_type: "line_item",
starts_at: moment().toISOString(),
ends_at: moment().add(10, "days").toISOString(),
title: 'TEST' + Math.floor(Math.random() * 1000),
value: -Number(25),
value_type: "percentage",
target_selection: "all"


And the response


{ HTTPError: Response code 422 (Unprocessable Entity)
    at (/Users/levivoelz/code/apps/tangible/tangible-api/node_modules/got/index.js:386:13)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)
  name: 'HTTPError',
  host: undefined,
  hostname: '',
  method: 'POST',
  path: '/admin/price_rules.json',
  protocol: 'https:',
  url: undefined,
  statusCode: 422,
  statusMessage: 'Unprocessable Entity',
   { server: 'nginx',
     date: 'Thu, 28 Mar 2019 18:04:49 GMT',
     'content-type': 'application/json; charset=utf-8',
     'transfer-encoding': 'chunked',
     connection: 'close',
     'x-sorting-hat-podid': '50',
     'x-sorting-hat-shopid': '6734250035',
     'referrer-policy': 'origin-when-cross-origin',
     'x-frame-options': 'DENY',
     'x-shopid': '6734250035',
     'x-shardid': '50',
     'x-stats-userid': '',
     'x-stats-apiclientid': '2397866',
     'x-stats-apipermissionid': '82519588915',
     http_x_shopify_shop_api_call_limit: '1/40',
     'x-shopify-shop-api-call-limit': '1/40',
     'strict-transport-security': 'max-age=7889238',
     'x-request-id': '2eeacd51-b7eb-4fa5-a3fa-34f29809e5f6',
     'x-shopify-stage': 'production',
     'content-security-policy': 'default-src \'self\' data: blob: \'unsafe-inline\' \'unsafe-eval\' https://* shopify-pos://*; block-all-mixed-content; child-src \'self\' https://* shopify-pos://*; connect-src \'self\' wss://* https://*; frame-ancestors \'none\'; img-src \'self\' data: blob: https:; script-src \'self\' \'unsafe-inline\' \'unsafe-eval\'; upgrade-insecure-requests; report-uri /csp-report?source%5Baction%5D=create&source%5Bapp%5D=Shopify&source%5Bcontroller%5D=admin%2Fprice_rules&source%5Bsection%5D=admin_api&source%5Buuid%5D=2eeacd51-b7eb-4fa5-a3fa-34f29809e5f6',
     'x-content-type-options': 'nosniff',
     'x-download-options': 'noopen',
     'x-permitted-cross-domain-policies': 'none',
     'x-xss-protection': '1; mode=block; report=/xss-report?source%5Baction%5D=create&source%5Bapp%5D=Shopify&source%5Bcontroller%5D=admin%2Fprice_rules&source%5Bsection%5D=admin_api&source%5Buuid%5D=2eeacd51-b7eb-4fa5-a3fa-34f29809e5f6',
     'x-dc': 'chi2,gcp-us-east1' } }


If I change the allocation method to "across", I get a successful response, but we need to use the allocation method: "each".


Per the API documentation, there are no restrictions on using our data configuration with "each". I believe this may be a bug or an undocumented change to the API. Also, per your status code documentation, there should be an error property in the response with more information, but I'm not seeing it in the response.


422 Unprocessable Entity
The request body was well-formed but contains semantical errors. The response body will provide more details in the errors or error parameters.



New Member
3 0 0

I'm not sure that this reply is of much value to you, the original poster, but it might be of some value to others that find this post.


suspect the reason why you were getting this error when using an allocation_method value of "each" is that there's no sensible distinction between the 'each' and 'across' allocation methods for a percentage discount and that, in some sense, the default value is "across" and thus that value is required for these discounts (price rules).


When adding a { price rule / discount code } manually thru the Shopify store admin pages, the option that seems to correspond to the allocation_method property is labeled "Only apply discount once per order". Checking the checkbox for that option seems to correspond to a property value of "across". This option tho isn't visible for percentage type discounts (price rules); only fixed-amount discounts.


And this all makes a lot of sense, tho it would be nice if it was documented explicitly. There's no obvious way to 'allocate' a percentage discount 'across' multiple ('entitled') line items. It would probably be clearer for API users tho if the default/valid/required value were "each" and, again, if that was documented explicitly in the API docs.