Can't create variant when creating product via rest APi

Solved
VolkensPremium
Tourist
5 1 0

Hi been struggling the past couple of days on how to create a product with variants:

POST DATA IN JSON FORMAT:

2021-10-21 16:28:17 --> {
"product": {
"title": "IPod Nano - 8GB",
"body_html": "It's the small iPod with a big idea: Video.",
"vendor": "Apple",
"product_type": "Cult Products",
"tags": "Emotive, Flash Memory, MP3, Music",
"images": [
{
"src": "http:\/\/example.com\/burton.jpg",
"width": 110,
"height": 140,
"position": 1
}
],
"options": {
"name": "Color",
"values": [
"Pink",
"Red",
"Green",
"Black"
]
},
"variants": [
{
"sku": "IPOD2008PINK",
"grams": 567,
"price": 199.99,
"title": "Pink",
"weight": 0.2,
"barcode": "1234_pink",
"option1": "Pink",
"position": 1,
"product_id": 632910392,
"weight_unit": "kg",
"requires_shipping": true,
"inventory_quantity": 10
}
]
}
}

 

 

 


RESPONSE FOR SHOPIFY- 2021-10-21 16:28:17 --> {
"product": {
"id": 7195451949205,
"title": "IPod Nano - 8GB",
"body_html": "It's the small iPod with a big idea: Video.",
"vendor": "Apple",
"product_type": "Cult Products",
"created_at": "2021-10-21T16:28:17-04:00",
"handle": "ipod-nano-8gb",
"updated_at": "2021-10-21T16:28:18-04:00",
"published_at": "2021-10-21T16:28:17-04:00",
"template_suffix": null,
"status": "active",
"published_scope": "web",
"tags": "Emotive, Flash Memory, MP3, Music",
"admin_graphql_api_id": "gid:\/\/shopify\/Product\/7195451949205",
"variants": [
{
"id": 41351236812949,
"product_id": 7195451949205,
"title": "Default Title",
"price": "0.00",
"sku": "",
"position": 1,
"inventory_policy": "deny",
"compare_at_price": null,
"fulfillment_service": "manual",
"inventory_management": null,
"option1": "Default Title",
"option2": null,
"option3": null,
"created_at": "2021-10-21T16:28:18-04:00",
"updated_at": "2021-10-21T16:28:18-04:00",
"taxable": true,
"barcode": null,
"grams": 0,
"image_id": null,
"weight": 0,
"weight_unit": "kg",
"inventory_item_id": 43446920020117,
"inventory_quantity": 0,
"old_inventory_quantity": 0,
"requires_shipping": true,
"admin_graphql_api_id": "gid:\/\/shopify\/ProductVariant\/41351236812949"
}
],
"options": [
{
"id": 9289332359317,
"product_id": 7195451949205,
"name": "Color",
"position": 1,
"values": [
"Default Title"
]
}
],
"images": [],
"image": null
}
}

 

Does someone can help me out is my data format is correct?

Response from shopify always return empty variant. Am using PHP and i encode the array with http_build_query before i sent it.

Thanks to anyone that can help me out.

Regards

Mart

Accepted Solution (1)

Accepted Solutions
VolkensPremium
Tourist
5 1 0

This is an accepted solution.

all good now

every thing works as expected

View solution in original post

Replies 2 (2)
VolkensPremium
Tourist
5 1 0

I figure it out there was a bug in my code

if (is_array($query)) $query = http_build_query($query);

change to:
if (is_array($query)) $query = json_encode($query);

and add the header:

$request_headers[] = 'Content-Type: application/json';

variant get created now, but still got an issue with image not updating.

regards

Mart

VolkensPremium
Tourist
5 1 0

This is an accepted solution.

all good now

every thing works as expected

View solution in original post