API: Manually set 'Position' property within a 'Collect' resource

Solved

API: Manually set 'Position' property within a 'Collect' resource

ifekaj
Visitor
2 0 0

I have the following code:

 

 

 

foreach ($collects as $collect) {
    $collectId = $collect['id'];
    $collectionId = $collect['collection_id'];

    $collectCreateUrl = "https://{$storeUrl}/admin/api/2023-10/collects.json";
    $data = [
        'collect' => [
            'product_id' => $productId,
            'collection_id' => $collectionId,
            'position' => 1
        ]
    ];
    curlRequest($collectCreateUrl, $accessToken, 'POST', $data);
}

 

 

 

 

The code above is supposed to create a 'collect' for a product within a collection and set the position property to 1. Instead, it will set the position to be the last position within the collection specified and send the product to the end of the collection. I'm starting to believe it is impossible to manually set the position. Is this an expected behaviour? I cannot find anything in the documentation confirms this.


Accepted Solution (1)

Liam
Shopify Staff
2873 312 821

This is an accepted solution.

Hi Ifekaj,

 

If you want to re-order the products within a collection, you'll need to use the collectionReorderProducts mutation in the GraphQL API. This allows you to specify new positions for the products in the collection. 

 

Hope this helps!

Liam | Developer Advocate @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

View solution in original post

Replies 2 (2)

Liam
Shopify Staff
2873 312 821

This is an accepted solution.

Hi Ifekaj,

 

If you want to re-order the products within a collection, you'll need to use the collectionReorderProducts mutation in the GraphQL API. This allows you to specify new positions for the products in the collection. 

 

Hope this helps!

Liam | Developer Advocate @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

ifekaj
Visitor
2 0 0

Brilliant! This worked like a charm. Your help is much appreciated