2

Trying to create a new Variant using the python lib.

After creating a shopify session I simply try to create a Variant instance as per the docs

session = shopify.Session('<My Url>')
session.token = self.provider_access_token
shopify.ShopifyResource.activate_session(session)

v=shopify.Variant(dict(price="20.00", option1="Second"))
v.save()

and get:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Users/yoda/.virtualenvs/myproject/src/shopify/shopify/base.py", line 151, in     save
    return super(ShopifyResource, self).save()
  File "/Users/yoda/.virtualenvs/myproject/lib/python2.6/site-packages/pyactiveresource    /activeresource.py", line 732, in save
    data=self.to_xml())
  File "/Users/yoda/.virtualenvs/myproject/lib/python2.6/site-packages/pyactiveresource    /connection.py", line 313, in post
    return self._open('POST', path, headers=headers, data=data)
  File "/Users/yoda/.virtualenvs/myproject/src/shopify/shopify/base.py", line 18, in     _open
    self.response = super(ShopifyConnection, self)._open(*args, **kwargs)
  File "/Users/yoda/.virtualenvs/myproject/lib/python2.6/site-packages/pyactiveresource    /connection.py", line 258, in _open
    response = Response.from_httpresponse(self._handle_error(err))
  File "/Users/yoda/.virtualenvs/myproject/lib/python2.6/site-packages/pyactiveresource    /connection.py", line 357, in _handle_error
    raise ResourceNotFound(err)
ResourceNotFound: HTTP Error 404: Not Found

Any thoughts or tips? :)

Thanks RCdH

stardog101
  • 41
  • 2

2 Answers2

3

Product Variants must have a product_id specified when list, count, or create actions. This can be seen in the Product Variant API docs because the product_id is part of the URL path. It is for this reason that a 404 error is returned from Shopify, because the path does not exist.

The using the shopify_python_api page on the Shopify Wiki has a section on prefix options which you may find helpful.

In your example you just need to change the line

v=shopify.Variant(dict(price="20.00", option1="Second"))

to

v=shopify.Variant(dict(price="20.00", option1="Second", product_id=product.id))

assuming you have a product to create a variant on in the variable product.

Dylan Smith
  • 1,502
  • 8
  • 9
  • Hi Dylan, thanks for the feedback. I will give that a shot.. but as part of the process i tried.. p=shopify.Product(xxxxx) p.variants.append(v) Which of course did not work.. so I manually need to specify the product_id always. Will update the test as directed and see what happens :) – stardog101 Jun 17 '12 at 08:46
  • hmmm well here is the next lot of weirdness: Hmm seems to be a char limit on stack overflow for comments.. that sucks a bit. – stardog101 Jun 17 '12 at 10:23
  • v = shopify.Variant(dict(sku='test1', product_id=92048506, price="20.00")) v.save() – stardog101 Jun 17 '12 at 10:24
  • Gives: Traceback (most recent call last): File "", line 1, in File "/Users/rossc/.virtualenvs/cartvine/src/shopify/shopify/base.py", line 151, in save return super(ShopifyResource, self).save() File "/Users/rossc/.virtualenvs/cartvine/lib/python2.6/site-packages/pyactiveresource/activeresource.py", line 737, in save self.errors.from_xml(err.response.body) File "/Users/rossc/.virtualenvs/cartvine/lib/python2.6/site-packages/pyactiveresource/activeresource.py", line 88, in from_xml xml_string, saveroot=True)['errors']['error'] KeyError: 'errors' – stardog101 Jun 17 '12 at 10:25
  • but the errors prop is empty.... v.errors v.errors.__dict__ {'base': variant(None), 'errors': {}} – stardog101 Jun 17 '12 at 10:26
  • Feel free to append text to your original post if it is too much to fit in the comments. Alternatively you could create a gist. – Dylan Smith Jun 17 '12 at 16:22
  • When trying to add two variants in that way I was able to reproduce the problem. I used `shopify.ShopifyResource.connection.response.body` to get the response body, which was `'\n\n Options are not unique\n\n'`. That is a bug in shopify. It should be `'\n\n Options are not unique\n\n'`. I'll look into fixing that regression in Shopify to avoid future confusion. – Dylan Smith Jun 17 '12 at 16:24
  • I have a fix ready to deploy on Monday for the this improper errors format regression, along with a regression test to avoid this happening again. Sorry for the confusion it has caused. – Dylan Smith Jun 17 '12 at 18:52
  • Hi Dylan, thanks for the tips and also for looking into the issue. It is great to see such turn around and I look forward to continuing with my integration! all the best! – stardog101 Jun 18 '12 at 04:44
0

You also need to pass the variant's ID along with the product_id

# get first product
product = shopify.Product.find()[0]

# get product's first variant
variant = product.variants[0]

# pass both the product and variant ids 
v=shopify.Variant(dict(price="20.00", product_id=product.id, id=variant.id))

# finally save the variant
v.save()
ChrisC
  • 916
  • 1
  • 10
  • 24