Set Cost of item through Python API

Solved
Fredydb
Excursionist
17 0 1

I am trying to set a variant’s cost price through the python API. But seem to have trouble accessing the cost attribute.

I have this so far:

new_product = shopify.Product()
new_product.title = "AAA"
new_product.handle = "aaa-product"
variant = shopify.Variant()
variant.title = "Bleu"
variant.price = "20.00"
variant.inventory_management = 'shopify'
variant.inventory_policy = 'deny'
new_product.variants = [variant]
new_product.save()
variant_quantity = '4'
for v in new_product.variants:
inv_item_id = v.inventory_item_id
shopify.InventoryLevel.set(LOCATION_ID, inv_item_id, variant_quantity)
inv_item_id.cost = "5.00"

new_product.save()

I get error
inv_item_id.cost = "5.00"
AttributeError: 'int' object has no attribute 'cost'

Can someone help?

Thanks in advance,

Fred


0 Likes
BStubbs
Shopify Partner
127 14 51

Hi @Fredydb ,

Welcome to the forums!

You haven’t defined the InventoryItem as an object, you’ve only set the InventoryItem.id to the variable, which of course has no attribute ‘cost’.

Hope that helps!

 

Was this helpful? Press like!
Did it fix the problem? Mark it as the solution for others!
Buy me a beer? Well, sure!
Fredydb
Excursionist
17 0 1

Thank you @BStubbs, much appreciated!

Slowly learning by grabbing bits here and there.
Could you hint me on how i could go about to define the InventoryItem as an object in this bit of code?


0 Likes
Greg_Kujawa
Shopify Partner
1016 83 237

While I haven't personally dabbled with the Python implementation of the API, I'd imagine it would go something like this.

new_inventory_item = shopify.InventoryItem()
BStubbs
Shopify Partner
127 14 51

This is an accepted solution.

Good try, @Greg_Kujawa ! That's close, but not exactly what is needed.

That would create a new instance of the class InventoryItem, what @Fredydb is trying to do is to access the instance of InventoryItem that already exists from the Product.

Have a look at the initial Exception, it says that type Int has no attribute, so already you can tell you are trying to set an attribute to an integer, which will never work. You also see that you get this error from a variable that you have named

inv_item_id

The "id" in this name would mean to me that you intended this to actually be the ID of instance of the InventoryItem not the instance itself. I think you just forget to add the next step which was to look up the instance from Shopify.

The Shopify Python API uses Pyactiveresource as the basis for its queries, and its recommended you have a good read of the docs for this.

https://github.com/Shopify/pyactiveresource

From that readme, you can find out that there is a general FIND query that looks a bit like this:

shopify.Endpoint.find('idOfTheObjectYouWantToAccess')

Where Endpoint could be any of the Objects that Shopify allows the API to access (Product, Asset, Event etc).

So, the best way to do this would probably be:

for v in new_product.variants:
    inv_item_id = v.inventory_item_id
    shopify.InventoryLevel.set(LOCATION_ID, inv_item_id, variant_quantity)
    inv_item = shopify.InventoryItem.find(inv_item_id)
    inv_item.cost = "5.00"
    inv_item.save()

new_product.save()

 

Was this helpful? Press like!
Did it fix the problem? Mark it as the solution for others!
Buy me a beer? Well, sure!
Fredydb
Excursionist
17 0 1

Thank you @BStubbs, very detailed and clear explanation. Beyond this particular question, I find the answer is very helpful to understanding the broader general  concept.