Product Search By Title Using API

For all those still wondering how to fix this.

  1. Title parameter in the API cannot be used to search products, it will only work if you pass the exact product name.
  2. For CORS, hit the API on the backend rather than the client side.
  3. This is not the optimum solution but the best way I could make it work so far is by looping through all the products using since_id and then filtering the results out. Here’s my code
$keyword = $request->keyword;
        $active = true;
        $productsArray = [];
        $sinceId = 0;

        //loop until 0 products are returned
        while($active){
                $products = hitCurl('/admin/api/2021-01/products.json?limit=250&since_id=' . $sinceId, 'GET', $curlFields = "");
            if(sizeof($products['products']) <= 0){
                //break the loop when result is empty
                $active = false;
            }

            //loop within the results and push the product to an array
            for ($i = 0; $i <= sizeof($products['products']) - 1; $i++) {
                array_push($productsArray, $products['products'][$i]);

                //update the since_id to the latest id in the response
                if($i == sizeof($products['products']) - 1){
                    $sinceId = $products['products'][$i]['id'];
                }
            }
        }

        //loop the productsArray and filter results using strpos (php)