Export All Shopify Product Variants into a CSV file with Python/Django

Datetime:2016-08-22 22:23:59          Topic: Django           Share

I was recently approached by a client asking for a custom Shopify app that exports specific details from all product variants (IDs, titles, price etc.) into a CSV file. The online store was already having more than 100 000 product variants and this number was increasing daily.

I decided to use Python (Django) for this project, but also developed the same functionality with PHP for testing purposes (so if you need a solution in PHP have a look at the end of the article).

To get a list of all products using the Shopify API you have to send a request to the Product resource . By default the response from Shopify will return 50 products. You can change this, but the maximum number of products you can get at once is 250, which in many cases is less than the total number of products available in the store.

So, what we can do is first find the number of products the Shopify store has and then divide it by a certain limit per page (we can set it, should be up to 250), so we can find the number of pages we will have to loop through.

Let’s have a look at a simple example. Imagine a Shopify store has 4000 products paginated by 50 per page. This means we will have to go through 4000/50 = 80 pages in order to get all products.

Then, on each page for each product we will loop through all its variants and collect the data we need (like ID, title, description etc.).

Finally we will save the information into a CSV file .

Here’s the final code (the snippet below is based on using the official Shopify API library for Python):

products_count = shopify.Product.count(published_status='published')
limit = 50
pages = math.ceil(products_count/limit)
 
variants_data = []
 
for i in range(1, pages + 1):
    products = shopify.Product.find(limit=limit, page=i, published_status='published')
    for productin products:
        for variantin product.variants:
            variants_data.append([product.title, variant.id, variant.price])
 
 
withopen('feed.csv', 'w') as csvfile:
    out = csv.writer(csvfile)
    for variant_datain variants_data:
        out.writerow(variant_data)

I’ve added published_status=’published’ on lines 1 and 8 to make sure we’re only fetching products that are published and not hidden from the Online Store.

In the example above we’re collecting the product title, variant ID and variant price for each product variant and exporting this into a CSV file called “feed.csv”. Each variant has 3 columns and is inserted on a separate row.

If you feel more confident developing with PHP here is how you can achieve the same functionality (the snippet below is based on using the Sandeep’s Shopify API client):

$products_count = $shopify('GET /admin/products/count.json', array('published_status'=>'published'));
$limit = 50;
$pages = ceil($products_count/$limit);
 
$file = fopen("feed.csv","w");
 
for($i=1; $i<=$pages; $i++) {
    $products = $shopify('GET /admin/products.json?'.$limit.'=50&page='.$i .'&published_status=published');
 
    foreach($productsas $product) {
        $product_title = $product['title'];
 
        foreach($product['variants'] as $variant) {
            $product_variant_id = $variant['id'];
            $product_variant_price = $variant['price']; 
 
            $variant_data = array($product_title, $product_variant_id, $product_variant_price);
            fputcsv($file, $variant_data);
        }
    }
}
 
fclose($file);

You can easily build upon any of the snippets above regarding your project needs.

Let me know if you have any questions or need further help on this.





About List