9

I'm working on native mobile application and want to display product after customer selected a category. I am able to get list of the product by category in rest request but that list don't have much details about the product.

Request : http://localhost/magento2/index.php/rest/V1/categories/24/products        

(24 is category ID)

Response : [{"sku":"WH01","position":1,"category_id":"24"},...]

Earlier in Magento 1.9 product list was something like

{
2: {
entity_id: "2"
type_id: "simple"
sku: "Levis Bagpack"
description: "Bagpack"
short_description: "Bagpack"
meta_keyword: null
name: "Levis Bagpack"
meta_title: null
meta_description: null
regular_price_with_tax: 45
regular_price_without_tax: 45
final_price_with_tax: 45
final_price_without_tax: 45
is_saleable: true
image_url: "http://172.16.8.24:8080/magento/media/catalog/product/cache/0/image/9df78eab33525d08d6e5fb8d27136e95/images/catalog/product/placeholder/image.jpg"
}-

What should I do to get more info about product so I can show image and other things in mobile app ?

  • how to invoke SOAP API (example: Customer API)? – Manoj Kumar Aug 5 '15 at 12:37
4

Maybe you can try the GET /V1/products/:sku REST API to get all the details (https://github.com/magento/magento2/blob/develop/app/code/Magento/Catalog/etc/webapi.xml#l36)

The returned value will be representation of \Magento\Catalog\Api\Data\ProductInterface (including the additional attributes) https://github.com/magento/magento2/blob/develop/app/code/Magento/Catalog/Api/Data/ProductInterface.php

Check \Magento\Catalog\Api\ProductRepositoryInterface::get which services the GET /V1/products/:sku REST API.

You can make multiple requests for all product SKUs.

OR

You can use the search API to fetch the entire list in a single request based on your criteria:

ex: http://localhost/magento2/index.php/rest/V1/products?searchCriteria[filter_groups][0][filters][0][field]=sku&searchCriteria[filter_groups][0][filters][0][value]=simple&searchCriteria[filter_groups][0][filters][1][field]=sku&searchCriteria[filter_groups][0][filters][1][value]=Simple2&searchCriteria[filter_groups][0][filters][0][condition_type]=eq&searchCriteria[current_page]=1&searchCriteria[page_size]=2

In this case products with SKUs - simple and Simple2 are being searched.

2

In addition (not as replacement) to the other answers, if after that you still are missing a piece of information, you can fill gaps by writing your own API functions: https://alankent.wordpress.com/2015/07/24/creating-a-new-rest-web-service-in-magento-2/

  • how to invoke SOAP API (example: Customer API)? – Manoj Kumar Aug 5 '15 at 12:39
2
define('BASEURL','http://localhost/magento20_0407/');

$apiUser = 'testUser'; 
$apiPass = 'admin123';
$apiUrl = BASEURL.'index.php/rest/V1/integration/admin/token';
/*
    Magento 2 REST API Authentication
*/
$data = array("username" => $apiUser, "password" => $apiPass);                                                                    
$data_string = json_encode($data);                       
try{
    $ch = curl_init($apiUrl); 
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
        'Content-Type: application/json',                                                                                
        'Content-Length: ' . strlen($data_string))                                                                       
    );       
    $token = curl_exec($ch);
    $token = json_decode($token);
    if(isset($token->message)){
        echo $token->message;
    }else{
        $key = $token;
    }
}catch(Exception $e){
    echo 'Error: '.$e->getMessage();
}


/*
    Get Product By SKU REST API Magento 2
    Use above key into header
*/
$headers = array("Authorization: Bearer $key"); 
//$requestUrl = BASEURL.'index.php/rest/V1/products/24-MB01';//24-MB01 is the sku.
//$requestUrl = BASEURL.'index.php/rest/V1/products?searchCriteria[page_size]=10';// get total 10 products
//$requestUrl = BASEURL.'index.php/rest/V1/categories/24/products';// 24 category id
$requestUrl = BASEURL.'index.php/rest/V1/products?searchCriteria=';//get all products

$ch = curl_init();
try{
    $ch = curl_init($requestUrl); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);   

    $result = curl_exec($ch);
    $result = json_decode($result);

    if(isset($result->message)){
        echo $result->message;
    }else{
        print_r($result);
    }
}catch(Exception $e){
    echo 'Error: '.$e->getMessage();
}

Similarly you can change $requestUrl and filter product list by category id and get product detail.

Please confirm whether it solves your problem or not. Else I'll post another solution.

1

In addition to what @airboss said, we're working on our 'integration' style of APIs. That's an API call that has extensible attributes add to it ( Catalog, Sales ).

In a few weeks you should be able to do a GET /V1/products/:sku REST API call and get everything related to that product ( if you're authorized as a customer you won't see the inventory object ). For example if you call a sku that's a bundled product you'd get the simple product, an array of product options, an array of product links, media urls, and the inventory object*. Same thing when you use the search ( we'll be calling it filter instead soon ) API.

-Chuck

  • the V1/products API does not include category_id parameter? ANy workaround for that ? – nr5 Dec 31 '15 at 12:03

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.