Skip to content

Unclear error when trying to create discount code #501

@nonfatalexec

Description

@nonfatalexec

Issue summary

Write a short description of the issue here ↓
I'm trying to create a discount code with this snippet of code.

price_rule = shopify.PriceRule.create({
    "price_rule_id" : 11111,
    "title" : "5_OFF_85_000001",
    "target_type" : "line_item",
    "target_selection" : "entitled",
    "allocation_method" : "across",
    "value_type" : "fixed_amount",
    "value" : "-5",
    "once_per_customer" : True,
    "prerequisite_subtotal_range" : {"greater_than_or_equal_to" : "85.0"},
    "customer_selection" : "all",
    "starts_at" : datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
    "ends_at" : (datetime.today() + timedelta(days=5)).strftime("%Y-%m-%d %H:%M:%S")
    })

discount_code = shopify.DiscountCode({
    "code" : "5_OFF_85-AAA111",
    "id" : int(time.time())
    })

discount_code_res = price_rule.add_discount_code(discount_code)

Expected behavior

What do you think should happen?
I expect the discount code to be created or an error message stating what is incorrect.

Actual behavior

What actually happens?
I get the below error which is unclear what is wrong with "price_rule_id".

DetailsTraceback (most recent call last): File "/home/gerald/shopify/lib/python3.9/site-packages/pyactiveresource/connection.py", line 286, in _open http_response = self._handle_error(self._urlopen(request)) File "/home/gerald/shopify/lib/python3.9/site-packages/pyactiveresource/connection.py", line 316, in _urlopen return urllib.request.urlopen(request, timeout=self.timeout) File "/usr/lib64/python3.9/urllib/request.py", line 214, in urlopen return opener.open(url, data, timeout) File "/usr/lib64/python3.9/urllib/request.py", line 523, in open response = meth(req, response) File "/usr/lib64/python3.9/urllib/request.py", line 632, in http_response response = self.parent.error( File "/usr/lib64/python3.9/urllib/request.py", line 561, in error return self._call_chain(*args) File "/usr/lib64/python3.9/urllib/request.py", line 494, in _call_chain result = func(*args) File "/usr/lib64/python3.9/urllib/request.py", line 641, in http_error_default raise HTTPError(req.full_url, code, msg, hdrs, fp) urllib.error.HTTPError: HTTP Error 400: Bad Request

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "", line 1, in
File "/home/gerald/shopify/lib/python3.9/site-packages/shopify/resources/price_rule.py", line 9, in add_discount_code
resource = self.post("discount_codes", discount_code.encode())
File "/home/gerald/shopify/lib/python3.9/site-packages/pyactiveresource/activeresource.py", line 1117, in _instance_post
return self.klass.connection.post(url, self.klass.headers, body)
File "/home/gerald/shopify/lib/python3.9/site-packages/pyactiveresource/connection.py", line 375, in post
return self._open('POST', path, headers=headers, data=data)
File "/home/gerald/shopify/lib/python3.9/site-packages/shopify/base.py", line 26, in _open
self.response = super(ShopifyConnection, self)._open(*args, **kwargs)
File "/home/gerald/shopify/lib/python3.9/site-packages/pyactiveresource/connection.py", line 288, in _open
http_response = self._handle_error(err)
File "/home/gerald/shopify/lib/python3.9/site-packages/pyactiveresource/connection.py", line 413, in _handle_error
raise BadRequest(err)
pyactiveresource.connection.BadRequest: Response(code=400, body="b'{"errors":{"price_rule_id":"expected String to be a id"}}'", headers={'Date': 'Fri, 26 Mar 2021 00:16:24 GMT', ..., msg="Bad Request")

Steps to reproduce the problem

  1. Run the snippet of code

Reduced test case

The best way to get your bug fixed is to provide a reduced test case.

Specifications

  • shopify_python_api version: 8.3.0
  • pyactiveresource version: 2.2.2
  • Shopify API version used (e.g. '2020-07'): 2021-01

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions