Development discussions around Shopify APIs
I am trying to publish a product via the rest API using javascript. The product is in draft status, but when I do the API call, I get a 200 but nothing happens and the product doesn't get published.
const url: string = `https://${user.store}.myshopify.com/admin/api/2021-04/products/6552072224831.json`
const result = await fetch(url, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'X-Shopify-Access-Token': user.accessToken
},
body: JSON.stringify({
"product": {
"id": 6552072224831,
"published": true
}
}),
});
This is the response which I get, you can see the published_at field is still null. I check in the admin UI and see that the product is still in draft.
"product": {
"id": 6552072224831,
"title": "Black Headphones",
"body_html": "Black headphones with red cord.",
"vendor": "Bins Group",
"product_type": "Industrial",
"created_at": "2021-03-10T03:18:13-05:00",
"handle": "black-headphones",
"updated_at": "2021-03-23T12:57:07-04:00",
"published_at": null,
"template_suffix": "",
"status": "draft",
"published_scope": "web",
"tags": "developer-tools-generator",
"admin_graphql_api_id": "gid://shopify/Product/6552072224831",
"variants": [
{
"id": 39263516131391,
"product_id": 6552072224831,
"title": "Steel",
"price": "49.00",
"sku": "",
"position": 1,
"inventory_policy": "deny",
"compare_at_price": null,
"fulfillment_service": "manual",
"inventory_management": "shopify",
"option1": "Steel",
"option2": null,
"option3": null,
"created_at": "2021-03-10T03:18:13-05:00",
"updated_at": "2021-03-10T03:18:13-05:00",
"taxable": true,
"barcode": null,
"grams": 67,
"image_id": null,
"weight": 67,
"weight_unit": "g",
"inventory_item_id": 41357419806783,
"inventory_quantity": 5,
"old_inventory_quantity": 5,
"requires_shipping": true,
"admin_graphql_api_id": "gid://shopify/ProductVariant/39263516131391"
},
{
"id": 39263516164159,
"product_id": 6552072224831,
"title": "azure",
"price": "15.00",
"sku": "",
"position": 2,
"inventory_policy": "deny",
"compare_at_price": null,
"fulfillment_service": "manual",
"inventory_management": "shopify",
"option1": "azure",
"option2": null,
"option3": null,
"created_at": "2021-03-10T03:18:13-05:00",
"updated_at": "2021-03-10T03:18:13-05:00",
"taxable": true,
"barcode": null,
"grams": 0,
"image_id": null,
"weight": 0,
"weight_unit": "g",
"inventory_item_id": 41357419839551,
"inventory_quantity": 49,
"old_inventory_quantity": 49,
"requires_shipping": false,
"admin_graphql_api_id": "gid://shopify/ProductVariant/39263516164159"
}
],
"options": [
{
"id": 8425136128063,
"product_id": 6552072224831,
"name": "Title",
"position": 1,
"values": [
"Steel",
"azure"
]
}
],
"images": [
{
"id": 27975178420287,
"product_id": 6552072224831,
"position": 1,
"created_at": "2021-03-10T03:18:13-05:00",
"updated_at": "2021-03-10T03:18:13-05:00",
"alt": null,
"width": 5142,
"height": 3428,
"src": "https://cdn.shopify.com/s/files/1/0089/6660/2815/products/black-headphones.jpg?v=1615364293",
"variant_ids": [],
"admin_graphql_api_id": "gid://shopify/ProductImage/27975178420287"
}
],
"image": {
"id": 27975178420287,
"product_id": 6552072224831,
"position": 1,
"created_at": "2021-03-10T03:18:13-05:00",
"updated_at": "2021-03-10T03:18:13-05:00",
"alt": null,
"width": 5142,
"height": 3428,
"src": "https://cdn.shopify.com/s/files/1/0089/6660/2815/products/black-headphones.jpg?v=1615364293",
"variant_ids": [],
"admin_graphql_api_id": "gid://shopify/ProductImage/27975178420287"
}
}
I looked at the graphql but it seems to only support publishing products if the store is on shopify plus, which is a pretty big limitation.
How do I go about determining why this product isn't publishing
What happens if you use "published_scope": "web" and "status": "active" in your request body instead?
when I set status to active then the product moves from draft to active status, which I guess is what I was looking for.
For clarification, if you have an active product but do published: false, what exactly happens to the product? Since I just did this and got a 200 response, and published_at is null but I can still see the product in the catalog?
Wondering what exactly publishing does and how it interacts with the active status?
Ha! Publishing is a mess right now. It seems only Plus stores allows for API calls to truly deal with publishing. Without Plus, all you can do is move products from Draft to Active to Archived and basically set their status to on.
Not exactly stellar but we live it.
If you set Published Scope to "web" or "global" and didn't set the published_at. The product will never get published.
So send both parameters
published_scope: global
published_at: {Current timestamp (ISO Format 8601}
thanks
This saved me. I wonder if it isn't mentioned anywhere in their docs. Thanks!
User | RANK |
---|---|
44 | |
16 | |
9 | |
9 | |
7 |
Thanks to all Community members that participated in our inaugural 2 week AMA on the new E...
By Jacqui Mar 10, 2023Upskill and stand out with the new Shopify Foundations Certification program
By SarahF_Shopify Mar 6, 2023One of the key components to running a successful online business is having clear and co...
By Ollie Mar 6, 2023