
During a recent project that we worked on, we were surprised to see that Shopify still doesn’t give you the ability to import or export collections from a store. But they do give you this option for other things such as project/customers and discount codes.
We were setting up a new Shopify store where we wanted to be specific about the collections that we wanted to use, all the collections names had been planned out and we decided that we were going to use Smart collections across the entire site so that the collections could be controlled by tags.
We had typed up all of the names that we were happy with in Excel but then we didn’t want to go through the process of creating all of these one by one in Shopify (that sounds painful). We needed a way to create these quickly as these might not even be the final collection names.
Below we used a CURL request in PHP to fetch all smart collections, loop through them and create a csv with the following columns: handle, title, description
$url = "https://testing.myshopify.com/admin/api/2021-01/smart_collections.json"; $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => "", CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 30, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => "GET", CURLOPT_HTTPHEADER => array( "Host: corston.myshopify.com", "X-Shopify-Access-Token: ".$accessToken, ), )); $response = curl_exec($curl); $err = curl_error($curl); curl_close($curl); $data = json_decode($response, true); $collections = $data['smart_collections']; $collection_array = array(); // loop through smart collections // foreach ($collections as $collection){ $handle = $collection['handle']; $title = $collection['title']; $description = $collection['body_html']; $collection = array($handle, $title, $description); array_push($collection_array, $collection); } // create a csv file $file = fopen("collections.csv","w"); foreach ($collection_array as $line) { fputcsv($file, $line); } fclose($file);
Now that we have a csv file with all of our existing collections, we were then able to edit them and make any changes to the names and handles as we needed to.
We then created a new csv that also had the Smart condition condition of tags and the tag needed for the product to end up in that collection.
We then used the API request to create smart collections using a loop in our CSV and this created 200 smart collections in a couple minutes.