How can I do a pagination on abandoned chechout API

Solved
DaviAmaral
Excursionist
26 4 3

I am looking for information on how to do pagination using the abandoned checkout API,

but I could not find it.

https://shopify.dev/api/admin/rest/reference/orders/abandoned-checkouts

someone could help me with that?

 

Accepted Solution (1)

Accepted Solutions
DaviAmaral
Excursionist
26 4 3

This is an accepted solution.

I found this solution in PHP.

 

    public function testepagination(){
        ini_set('max_execution_time', 99999999);
        $limit = 250;
        $limit = '?limit=' . $limit;
        $merged = array();
        $next_page = '';
        $last_page = false;
        $i=0;
        $no_pagination = false;
        $page_info = '';
        $username = 'yourapikey';
        $password = 'yourapipassword';
        $shop = 'youshopify';
        $api = 'api/2021-10/';
        $created_at_min = '2020-01-01T00:00:00-00:01';
        $created_at_max= '2020-12-31T11:59:59-00:01';
        $auth = 'https://' . $username . ':' . $password . '@' . $shop . '.myshopify.com/admin/' . $api  ;
        $request = $auth . 'checkouts.json'. '?created_at_min='.$created_at_min.'&created_at_max='.$created_at_max;
        $fields = '';
        $endpoints = '';
        $x=1;
        $j=1;
        $data[0] = ['id', 'token', 'cart_token', 'email',  'buyer_accepts_marketing', 'created_at',  'updated_at',  'referring_site', 'taxes_included',  'currency', 'first_name', 'address1',  'address2', 'city',  'zip', 'province', 'country', 'last_name',  'country_code', 'province_code',  'name','abandoned_checkout_url'  ];

        $data2[0] = ['checkout_id', 'key', 'destination_location_id','fulfillment_service', 'gift_card', 'origin_location_id', 'presentment_title',  'presentment_variant_title',  'product_id',  'sku', 'title',   'variant_id',    'variant_title',   'vendor', 'line_price', 'price', 'quantity'];
        while(!$last_page) {
            sleep(1);
            $url = $request . $limit . $fields . $endpoints . $page_info;
            echo ($url. '<br>');
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_HEADERFUNCTION, function($curl, $header) use (&$headers) {
                $len = strlen($header);
                $header = explode(':', $header, 2);
                if (count($header) >= 2) {
                    $headers[strtolower(trim($header[0]))] = trim($header[1]);
                }
                return $len;
            });
            $result = curl_exec($curl);
            $array = json_decode($result);         
            if (isset($array->checkouts)){
                foreach ($array->checkouts as $key=>$value){
                                $data[$j] = [$value->id,
                                            $value->token,
                                            $value->cart_token,
                                            $value->email,
                                            $value->buyer_accepts_marketing,
                                            $value->created_at,
                                            $value->updated_at,
                                            $value->referring_site,
                                            $value->taxes_included,
                                            $value->currency,
                                            (isset( $value->shipping_address->first_name  )) ? $value->shipping_address->first_name   : '',
                                            (isset( $value->shipping_address->address1  )) ? $value->shipping_address->address1   : '',
                                            (isset( $value->shipping_address->address2  )) ? $value->shipping_address->address2   : '',
                                            (isset( $value->shipping_address->city  )) ? $value->shipping_address->city   : '',
                                            (isset( $value->shipping_address->zip  )) ? $value->shipping_address->zip   : '',
                                            (isset( $value->shipping_address->province  )) ? $value->shipping_address->province   : '',
                                            (isset( $value->shipping_address->country  )) ? $value->shipping_address->country   : '',
                                            (isset( $value->shipping_address->last_name  )) ? $value->shipping_address->last_name   : '',
                                            (isset( $value->shipping_address->country_code  )) ? $value->shipping_address->country_code   : '',
                                            (isset( $value->shipping_address->province_code  )) ? $value->shipping_address->province_code   : '',
                                            (isset( $value->shipping_address->name  )) ? $value->shipping_address->name   : '',
                                            (isset( $value->abandoned_checkout_url  )) ? $value->abandoned_checkout_url   : ''
                                        ];
                            $j++;                                      
                           
                            foreach ($value->line_items as $key=>$value2){
                                $data2[$x] = [$value->id,
                                                    $value2->key,
                                                    $value2->destination_location_id,
                                                    $value2->fulfillment_service,
                                                    $value2->gift_card,
                                                    $value2->origin_location_id,
                                                    $value2->presentment_title,
                                                    $value2->presentment_variant_title,
                                                    $value2->product_id,
                                                    $value2->sku,
                                                    $value2->title,
                                                    $value2->variant_id,
                                                    $value2->variant_title,
                                                    $value2->vendor,
                                                    $value2->line_price,
                                                    $value2->price ,
                                                    $value2->quantity];
                                $x++;    
                            }    
                }  
            }
                echo ("page" . $i++ . '<br>');
                curl_close($curl);
   
            if(isset($headers['link'])) {
                $links = explode(',', $headers['link']);
   
                foreach($links as $link) {
                    $next_page = false;
                    if(strpos($link, 'rel="next"')) {
                        $next_page = $link;
                    }
                }
   
                if($next_page) {
                    preg_match('~<(.*?)>~', $next_page, $next);
                    $url_components = parse_url($next[1]);
                    parse_str($url_components['query'], $params);
                    $page_info = '&page_info=' . $params['page_info'];
                    $request = $auth . 'checkouts.json';
                    echo $params['page_info'] . '<br>';
                    $endpoints = ''; // Link pagination does not support endpoints on pages 2 and up
                } else {
                    $last_page = true; // There's no next page, we're at the last page
                }
               } else {
                $last_page = true; // Couldn't find parameter link in headers, stop loop
            }
            if($no_pagination) {
                $last_page = true;
            }
        }
        $fp = fopen('checkouts.csv', 'w');
        // Loop through file pointer and a line
        foreach ($data as $fields) {
            fputcsv($fp, $fields,',');
        }
        fclose($fp);

        $fp = fopen('checkouts_lines.csv', 'w');
        // Loop through file pointer and a line
        foreach ($data2 as $fields) {
            fputcsv($fp, $fields,',');
        }
        fclose($fp);
    }

View solution in original post

Reply 1 (1)
DaviAmaral
Excursionist
26 4 3

This is an accepted solution.

I found this solution in PHP.

 

    public function testepagination(){
        ini_set('max_execution_time', 99999999);
        $limit = 250;
        $limit = '?limit=' . $limit;
        $merged = array();
        $next_page = '';
        $last_page = false;
        $i=0;
        $no_pagination = false;
        $page_info = '';
        $username = 'yourapikey';
        $password = 'yourapipassword';
        $shop = 'youshopify';
        $api = 'api/2021-10/';
        $created_at_min = '2020-01-01T00:00:00-00:01';
        $created_at_max= '2020-12-31T11:59:59-00:01';
        $auth = 'https://' . $username . ':' . $password . '@' . $shop . '.myshopify.com/admin/' . $api  ;
        $request = $auth . 'checkouts.json'. '?created_at_min='.$created_at_min.'&created_at_max='.$created_at_max;
        $fields = '';
        $endpoints = '';
        $x=1;
        $j=1;
        $data[0] = ['id', 'token', 'cart_token', 'email',  'buyer_accepts_marketing', 'created_at',  'updated_at',  'referring_site', 'taxes_included',  'currency', 'first_name', 'address1',  'address2', 'city',  'zip', 'province', 'country', 'last_name',  'country_code', 'province_code',  'name','abandoned_checkout_url'  ];

        $data2[0] = ['checkout_id', 'key', 'destination_location_id','fulfillment_service', 'gift_card', 'origin_location_id', 'presentment_title',  'presentment_variant_title',  'product_id',  'sku', 'title',   'variant_id',    'variant_title',   'vendor', 'line_price', 'price', 'quantity'];
        while(!$last_page) {
            sleep(1);
            $url = $request . $limit . $fields . $endpoints . $page_info;
            echo ($url. '<br>');
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_HEADERFUNCTION, function($curl, $header) use (&$headers) {
                $len = strlen($header);
                $header = explode(':', $header, 2);
                if (count($header) >= 2) {
                    $headers[strtolower(trim($header[0]))] = trim($header[1]);
                }
                return $len;
            });
            $result = curl_exec($curl);
            $array = json_decode($result);         
            if (isset($array->checkouts)){
                foreach ($array->checkouts as $key=>$value){
                                $data[$j] = [$value->id,
                                            $value->token,
                                            $value->cart_token,
                                            $value->email,
                                            $value->buyer_accepts_marketing,
                                            $value->created_at,
                                            $value->updated_at,
                                            $value->referring_site,
                                            $value->taxes_included,
                                            $value->currency,
                                            (isset( $value->shipping_address->first_name  )) ? $value->shipping_address->first_name   : '',
                                            (isset( $value->shipping_address->address1  )) ? $value->shipping_address->address1   : '',
                                            (isset( $value->shipping_address->address2  )) ? $value->shipping_address->address2   : '',
                                            (isset( $value->shipping_address->city  )) ? $value->shipping_address->city   : '',
                                            (isset( $value->shipping_address->zip  )) ? $value->shipping_address->zip   : '',
                                            (isset( $value->shipping_address->province  )) ? $value->shipping_address->province   : '',
                                            (isset( $value->shipping_address->country  )) ? $value->shipping_address->country   : '',
                                            (isset( $value->shipping_address->last_name  )) ? $value->shipping_address->last_name   : '',
                                            (isset( $value->shipping_address->country_code  )) ? $value->shipping_address->country_code   : '',
                                            (isset( $value->shipping_address->province_code  )) ? $value->shipping_address->province_code   : '',
                                            (isset( $value->shipping_address->name  )) ? $value->shipping_address->name   : '',
                                            (isset( $value->abandoned_checkout_url  )) ? $value->abandoned_checkout_url   : ''
                                        ];
                            $j++;                                      
                           
                            foreach ($value->line_items as $key=>$value2){
                                $data2[$x] = [$value->id,
                                                    $value2->key,
                                                    $value2->destination_location_id,
                                                    $value2->fulfillment_service,
                                                    $value2->gift_card,
                                                    $value2->origin_location_id,
                                                    $value2->presentment_title,
                                                    $value2->presentment_variant_title,
                                                    $value2->product_id,
                                                    $value2->sku,
                                                    $value2->title,
                                                    $value2->variant_id,
                                                    $value2->variant_title,
                                                    $value2->vendor,
                                                    $value2->line_price,
                                                    $value2->price ,
                                                    $value2->quantity];
                                $x++;    
                            }    
                }  
            }
                echo ("page" . $i++ . '<br>');
                curl_close($curl);
   
            if(isset($headers['link'])) {
                $links = explode(',', $headers['link']);
   
                foreach($links as $link) {
                    $next_page = false;
                    if(strpos($link, 'rel="next"')) {
                        $next_page = $link;
                    }
                }
   
                if($next_page) {
                    preg_match('~<(.*?)>~', $next_page, $next);
                    $url_components = parse_url($next[1]);
                    parse_str($url_components['query'], $params);
                    $page_info = '&page_info=' . $params['page_info'];
                    $request = $auth . 'checkouts.json';
                    echo $params['page_info'] . '<br>';
                    $endpoints = ''; // Link pagination does not support endpoints on pages 2 and up
                } else {
                    $last_page = true; // There's no next page, we're at the last page
                }
               } else {
                $last_page = true; // Couldn't find parameter link in headers, stop loop
            }
            if($no_pagination) {
                $last_page = true;
            }
        }
        $fp = fopen('checkouts.csv', 'w');
        // Loop through file pointer and a line
        foreach ($data as $fields) {
            fputcsv($fp, $fields,',');
        }
        fclose($fp);

        $fp = fopen('checkouts_lines.csv', 'w');
        // Loop through file pointer and a line
        foreach ($data2 as $fields) {
            fputcsv($fp, $fields,',');
        }
        fclose($fp);
    }