Help with cursor based paging

Solved
Highlighted
Tourist
4 0 3

I was confused by this for a bit too. When testing with curl, I was outputting the header info into the browser and Link: appeared empty, but when I viewed Inspect Element > Elements, I found the links were actually there in the html. Just need to parse the headers and grab them. This thread helped but accepted answer didn't mention this directly so I wanted to emphasize.

 

I personally like how Facebook does it with concatenating a json pagination object to the end of the api response. Good step in the right direction though, it does work!

0 Likes
Highlighted
Shopify Partner
4 0 2

I am not a fan of the way the cursor-based pagination has been implemented but we have to live with this.

 

The documentation clearly missing two vital things to confuse developers.

1. If there are fewer results than the limit the Link header will not be present.

2. The Link header can't be shown in the browser. If this chain was not there I would never get to know this to inspect the header.

 

Thanks @tbirnseth 

 

2 Likes
Highlighted
Tourist
5 0 1

Why it is marked as Answered.  I have the same issue.

0 Likes
Highlighted
Tourist
4 0 3

Please check your html. The links are there, they may not be rendering properly in the browser window when testing but they are there. Let us know what you've tried and what outcome you're currently getting if still having an issue!

0 Likes
Highlighted
Tourist
5 0 1

OK. I found that. but how to get the link with php. Is there a simple way to get the link to a variable?

0 Likes
Highlighted
Tourist
4 0 3

You have to isolate the request headers into a variable. If using Curl, something like CURLOPT_HEADER=>1 in the request. Look up some code to isolate curl header into a variable and  then return it from a function. 

 

Something like:

 

// Shopify Get Products API Call
$response = $shopify->getShopifyRemoteUrlWithHeaders( $shopify_api_endpoint );
 
// Get Response Headers
$headers = $shopify->getHeadersFromCurlResponse($response);
 
Then just parse, something like:
 

$link_ex = explode( '<', $headers['Link'] );
$link_previous_ex = explode( '>;', $link_ex[1] );
$link_next_ex = explode( '>;', $link_ex[2] );
$link_previous = $link_previous_ex[0];
$link_next = $link_next_ex[0];

2 Likes
Highlighted
Shopify Partner
9 0 9
I'm sure there's a more elegant solution. However this is what I use. 
The function parameter should be an empty string on the first call and
the desired direction on subsequent calls (I.e. 'next', 'prev').  It
will return the query string for the URL you're using.  If you want the
full URL then make appropriate changes by returning $url instead.

    private static function get_qs($kind='next') {
        if( empty($kind) )    {    // first pass
            return sprintf("limit=%d", self::$items_per_page);
        }

        $headers = Http::getHeaders();
        $headers_ar = explode("\r\n", $headers);    // explode each
line
        foreach($headers_ar as $_v) {    // loop through the lines
            if( strpos($_v, 'link:') !== false) {    // found the
'link' line
                // Each link header has one or more link values in <>'s
                $x = explode('<', $_v);
                // $x[0] contains the 'link:' header tag
                array_shift($x);    // throw it away
                foreach($x as $rel) {
                    if( strpos($rel, "rel=\"$kind\"")) {
                        // Found the kind ww're looking for
                        $z = explode('>', $rel);    // Now just get
the URL portion into $z[0]
                        $url = $z[0];
                        $q = explode('?', $url);
                        return empty($q[1]) ? '' : $q[1];
                    }
                }
            }
        }
        return '';
    }

1 Like
Highlighted
Shopify Partner
10 0 0

@tbirnseth @DebashisD @DanteCullari @Josh @Manaspaul623 

 

Hi guys.

 

I am not a developer but trying to troubleshoot with my developer about why since implementing the new cursor-based pagination in our app, users can't search partially for products when filtering.  Previously they could, and now I am considering reverting to the old api until this is figured out.

 

Is the solution shown here related in any way to our issue?

0 Likes
Highlighted
Tourist
4 0 3

Hey I'm not 100% sure what you mean by "can't search partially for products when filtering", but maybe Shopify's predictive search api will help:

 

https://shopify.dev/tutorials/add-predictive-search-to-your-shopify-theme#requesting-predictive-sear...

 

I think using the products endpoint for search would be very inefficient if you have a lot of products, although it's doable. There are a lot of solutions I'm sure your dev should be able to come up.

 

Couple solutions off top of my head - store all the products in your own database and maintain updates with daily syncs from Shopify - perform your existing call with all products as response. Or you could technically call the products endpoint, loop through all of the pages, store search matches in memory and return, but this again would be pretty inefficient. Or again you could sever filtering from search and use the predictive search api for the search portion. Again not really sure what you're trying to accomplish but I've actually gone with my own database that syncs daily and it has been very useful for a lot of other things.

1 Like
Highlighted
Shopify Partner
10 0 0

Our app is backend. We allow users to apply bulk price updates to their products. They first need to search and filter through to find the products they want to update. Before they could search 'Blue' and the search would bring up all products with 'blue' in the title. Now that doesn't work.

 

We are using REST API.

 

0 Likes