I’m stuck trying to set up a custom product collection using the Shopify API. I’ve managed to make other API calls work fine, like showing products and carts. But when I try to make a custom collection with some product IDs I know are good, it’s not working out.
Here’s a bit of my code:
$new_collection = [
'custom_collection' => [
'name' => 'Summer Sale',
'products' => [
'id' => 123456789,
'id' => 987654321
]
]
];
try {
$result = $shopify_client->post('/admin/custom_collections.json', $new_collection);
echo 'API calls left: ' . $shopify_client->calls_remaining();
} catch (ShopifyException $e) {
echo 'Oops, something went wrong';
print_r($e->getMessage());
}
When I run this, I get an error that says ‘Internal Server Error’ with a 500 status code. I’ve been following Shopify’s example code, but I’m not making much progress. Any ideas on what I might be doing wrong or how to fix this? Thanks for any help you can give!
hey laura, i ran into this too. the thing is, u cant add products when making the collection - do it after creation.
create collection first:
$new_collection = [
'custom_collection' => [
'title' => 'Summer Sale'
]
];
$result = $shopify_client->post('/admin/custom_collections.json', $new_collection);
then add products:
$shopify_client->post('/admin/collects.json', ['collect' => [
'collection_id' => $result['custom_collection']['id'],
'product_id' => 123456789
]]);
hope that helps!
I’ve dealt with this issue before, and it’s a common stumbling block. The problem lies in how you’re structuring the ‘products’ array. Shopify doesn’t allow direct product assignment during collection creation.
Instead, create the collection first, then add products separately:
$new_collection = [
'custom_collection' => [
'title' => 'Summer Sale'
]
];
$result = $shopify_client->post('/admin/custom_collections.json', $new_collection);
$collection_id = $result['custom_collection']['id'];
$product_ids = [123456789, 987654321];
foreach ($product_ids as $product_id) {
$collect = [
'collect' => [
'collection_id' => $collection_id,
'product_id' => $product_id
]
];
$shopify_client->post('/admin/collects.json', $collect);
}
This approach should resolve your 500 error. Remember to handle exceptions for each API call.
I’ve encountered similar issues when working with the Shopify API for custom collections. From my experience, the problem might be in how you’re structuring the ‘products’ array. Instead of listing products directly in the collection creation, you typically need to add products to the collection after it’s created.
Try creating the collection first without products:
$new_collection = [
'custom_collection' => [
'title' => 'Summer Sale'
]
];
$result = $shopify_client->post('/admin/custom_collections.json', $new_collection);
$collection_id = $result['custom_collection']['id'];
Then, add products to the collection using the Collect API:
$product_ids = [123456789, 987654321];
foreach ($product_ids as $product_id) {
$collect = [
'collect' => [
'collection_id' => $collection_id,
'product_id' => $product_id
]
];
$shopify_client->post('/admin/collects.json', $collect);
}
This approach has worked well for me in the past. Hope it helps!