ben olsen
ben olsen

Reputation: 703

Using Shopify API Python, add new product with price and 'requires_shipping': False

I am trying to add a new product through the Python Shopify API. I know how to add title and body and a picture but I am having issues with adding price and I need to have 'requires_shipping': False. I can't find anywhere how to achieve this.

This is what I have so far.

import shopify    
API_KEY = 'dsfsdsdsdsdsad'
PASSWORD = 'sadsdasdasdas'

shop_url = "https://%s:%[email protected]/admin" % (API_KEY, PASSWORD)
shopify.ShopifyResource.set_site(shop_url)



path = "audi.jpg"

new_product = shopify.Product()
new_product.title = "Audi pictures test "
new_product.body_html = "body of the page <br/><br/> test <br/> test"

###########this part is so far good. but the bottom part is not working#### 

variant = shopify.Variant(price=9.99)) # this does not work
new_product.variant() # this does not work
variant_2 = shopify.Variant(requires_shipping=False) #this does not work
new_product.variant_2() This does not work 



image = shopify.Image()

with open(path, "rb") as f:
    filename = path.split("/")[-1:][0]
    encoded = f.read()
    image.attach_image(encoded, filename=filename)

new_product.images = [image] # Here's the change
new_product.save()

Upvotes: 1

Views: 4229

Answers (1)

Jamie Dwyer
Jamie Dwyer

Reputation: 214

Only prefix options (e.g. product_id for Variants, order_id for Fulfillments) should be passed as explicit parameters to the constructor. If you want to initialize the attributes of the resource you'll need to pass them in as a dict.

You're also not associating your new variant with your new product at any point.

This should help:

new_product = shopify.Product()
new_product.title = "Shopify Logo T-Shirt"
new_product.body_html = "<b>Test description</b>"
variant = shopify.Variant({'price': 9.99, 'requires_shipping': False})
new_product.variants = [variant]
new_product.save()
=> True

You can also specify the attributes of the resource after initializing, as you're already doing for the Product resource.

variant = shopify.Variant()
variant.price = 9.99
variant.requires_shipping = False

Another option would be to save the product first and initialize the variant by passing the product_id explicitly, e.g.

shopify.Variant(product_id=1234567)

Take a look at the README for more usage examples.

Upvotes: 5

Related Questions