1

I need one help. I need to get all product details using multiple product id/sku in array using REST API. I am explaining my code below.

$pid=[23,24,25]
foreach ($pid as $v) {
        $ch = curl_init("http://example.com/index.php/rest/V1/products?searchCriteria[filterGroups][0][filters][0][field]=entity_id&searchCriteria[filterGroups][0][filters][0][condition_type]=eq&searchCriteria[filterGroups][0][filters][0][value]=".$v);


        //curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        //curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));
        $resp = curl_exec($ch);
        $resup = json_decode($resp, 1);
        $allProdArr[]=$resup;
    }

Here I am using loop for that reason also the response is coming late. Here I need Is there any REST API which can take the direct array of ids/sku and give the response in a single request. By using loop and calling passing the request each time give the late response which is not user friendly. Please help me to resolve this issue.

1

Try below code:

    $pids=[23,24,25]
    $ch = curl_init("http://example.com/index.php/rest/V1/products?searchCriteria[filterGroups][0][filters][0][field]=entity_id&searchCriteria[filterGroups][0][filters][0][condition_type]=in&searchCriteria[filterGroups][0][filters][0][value]=".implode(',',$pids));

    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $resp = curl_exec($ch);
    $resup = json_decode($resp, 1);
    $allProdArr[]=$resup;
  • OK i will test and let you know. – satya Sep 5 '18 at 7:44
  • I tested it and issue is its ignoring the 1st value of array and also if array has only one value it did not give any response. – satya Sep 5 '18 at 9:14
  • I have modified the code and it is working perfectly as tested. – Prasanta Hatui Sep 5 '18 at 9:41
  • Ok,its working but one thing missing here i.e- stock count(['stock_item']['qty']). This part is not coming. – satya Sep 5 '18 at 9:51
  • To get stock information you have to call stock related api by sku. – Prasanta Hatui Sep 5 '18 at 9:53
0

This approach can help in your case, just go through detail of this article: Get product collection with product ids

0

You have to add your array in searchCriteria value and add the condition to check if entity_id is in your array or not.

Try the following code.

$ids = [1,2,3,4,5];
$ids = '['.implode(',', $ids).']';
$restResourceUri = "http://www.example.com/index.php/rest/V1/products?searchCriteria[filter_groups][0][filters][0][field]=entity_id&searchCriteria[filter_groups][0][filters][0][value]=$ids&searchCriteria[filter_groups][0][filters][0][condition_type]=in";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.