Setting Cost Per Item via API

Highlighted

Hi All,

I am looking for some assistance with setting the Cost Per Item. I have tried this a fw times and am not getting anywhere. As a test, just to get the actual JSON right, I am using Shopify Dev Tools to submit a new product to a store. Typically, creating a new product looks like this:

{
  "product": {
    "title": "Burton Custom Freestyle 151",
    "body_html": "<strong>Good snowboard!</strong>",
    "vendor": "Burton",
    "product_type": "Snowboard",
    "tags": "Barnes & Noble, John's Fav, \"Big Air\""
  }
}

Now, the documentation instead says we need to submit this within the variant object, so I have set it up as below:

{
  "product": {
    "title": "Burton Custom Freestyle 151",
    "body_html": "<strong>Good snowboard!</strong>",
    "vendor": "Burton",
    "product_type": "Snowboard",
    "variants": [
      {
        "option1": "default",
        "price": "10.00",
        "sku": "123",
        "inventoryItem": {
           "cost": "25.00"
        }
      }
    ]
  }
}

When posted, I get a nice 201 Response and the following back:

{
  "product": {
    "id": 1905703518265,
    "title": "Burton Custom Freestyle 151",
    "body_html": "<strong>Good snowboard!</strong>",
    "vendor": "Burton",
    "product_type": "Snowboard",
    "created_at": "2018-12-13T07:16:32-05:00",
    "handle": "burton-custom-freestyle-153",
    "updated_at": "2018-12-13T07:16:32-05:00",
    "published_at": "2018-12-13T07:16:32-05:00",
    "template_suffix": null,
    "tags": "",
    "published_scope": "web",
    "admin_graphql_api_id": "gid://shopify/Product/1905703518265",
    "variants": [
      {
        "id": 18383254716473,
        "product_id": 1905703518265,
        "title": "default",
        "price": "10.00",
        "sku": "123",
        "position": 1,
        "inventory_policy": "deny",
        "compare_at_price": null,
        "fulfillment_service": "manual",
        "inventory_management": null,
        "option1": "default",
        "option2": null,
        "option3": null,
        "created_at": "2018-12-13T07:16:32-05:00",
        "updated_at": "2018-12-13T07:16:32-05:00",
        "taxable": true,
        "barcode": null,
        "grams": 0,
        "image_id": null,
        "weight": 0,
        "weight_unit": "kg",
        "inventory_item_id": 20346529677369,
        "inventory_quantity": 0,
        "old_inventory_quantity": 0,
        "requires_shipping": true,
        "admin_graphql_api_id": "gid://shopify/ProductVariant/18383254716473"
      }
    ],
    "options": [
      {
        "id": 2809924059193,
        "product_id": 1905703518265,
        "name": "Title",
        "position": 1,
        "values": [
          "default"
        ]
      }
    ],
    "images": [],
    "image": null
  }
}

In the actual store, everything also looks good, except the Cost of Item has not be set. I also do not see the cost per item in the response from Shopify. 

Just trying to figure out what I am doing wrong here. Any help would be greatly appreciated. 

Max Sadlowski - CEO - BentoSMB - Shopify Expert
0 Likes
Highlighted
Shopify Partner
1840 177 711

Hi max,

inventoryItem is not a valid property in the product object returned by the product endpoint. Whether as a response or requests body. The properties you can use are described in the documentation.

That said, you would want to

  1. create the product (without the inventoryItem bit)
  2. the response will include the inventory_item_id
  3. using that inventory_item_id you will want to update the inventory item to include your cost PUT /admin/inventory_items/#{inventory_item_id}.json
{
  "inventory_item": {
    "cost": "25.00"
  }
}

Hope this helps!

Liked this post? You might also like our fantastic upsell apps Candy Rack and Candy Cart or offer free gifts with Gift Box. All made with ❤️  and care by Digismoothie
1 Like
Highlighted

Hi Karl,

Thanks for the response, I figured this out as well after reading a bit more. I was looking at the GraphL documentation not the REST docs. Shame that I have to do two calls for this, but that is another reason to move over to GraphQL instead.

Thanks!

Max Sadlowski - CEO - BentoSMB - Shopify Expert
0 Likes
Highlighted
New Member
7 0 0

I had the same question, but now you can set the price and cost with one request when creating the product. Please see below.

See the node product > variants > [cost, price]

Create the product with price 199.99 and cost 10.99:

POST https://your-shop-name.myshopify.com/admin/api/2020-04/products.json

Headers:

X-Shopify-Access-Token: Your access token here

Content-Type: application/json

JSON Body:

{
	"product": {
		"title": "Item1 Test - Burton Custom Freestyle 151",
		"body_html": "<strong>Good snowboard!</strong>",
		"vendor": "amrod",
		"product_type": "",
		"published": true,
		"options": [
			{
				"name": "Colors"
			},
			{
				"name": "Sizes"
			}
		],
		"variants": [
			{
				"cost": 10.99,
				"price": 199.99,
				"option1": "Blue",
				"option2": "155"
			},
			{
				"cost": 10.99,
				"price": 199.99,
				"option1": "Black",
				"option2": "159"
			}
		],
		"images": [
			{
				"src": "https://homepages.cae.wisc.edu/~ece533/images/cat.png"
			}
		],
		"tags": [
			"tag1"
		],
		"metafields": [
			{
				"key": "new",
				"value": "newvalue",
				"value_type": "string",
				"namespace": "global"
			}
		]
	}
}

 

Get the variant:

Verify that the cost of 10.99 has been set, use your product > variants > inventory_item_id found in your response when creating the product above:

GET https://your-shop-name.myshopify.com/admin/api/2020-04/inventory_items/123.json 

 

{
  "inventory_item": {
    "id": 123,
    "sku": "",
    "created_at": "2020-07-10T16:08:49+02:00",
    "updated_at": "2020-07-10T16:08:49+02:00",
    "requires_shipping": true,
    "cost": "10.99",
    "country_code_of_origin": null,
    "province_code_of_origin": null,
    "harmonized_system_code": null,
    "tracked": false,
    "country_harmonized_system_codes": [],
    "admin_graphql_api_id": "gid:\/\/shopify\/InventoryItem\/123"
  }
}

 

0 Likes