如何获取产品列表及其详细信息,其中apse后端是magento2

时间:2015-04-09 09:23:56

标签: rest magento magento2

我在本机移动应用程序后端工作是magento2,我希望在客户选择一个类别后显示产品。我可以在休息请求中按类别获取产品列表,但该列表没有关于该产品的详细信息。

  

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

(24是类别ID)

  

回复:[{" sku":" WH01","职位":1," category_id":" 24& #34;},...]

Magento 1.9早期产品清单就像是



       {
    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"
    }

我应该怎样做才能获得有关产品的更多信息,以便在移动应用中显示图片和其他内容?

4 个答案:

答案 0 :(得分:4)

也许您可以尝试使用GET / V1 / products /:sku REST API来获取所有详细信息(https://github.com/magento/magento2/blob/develop/app/code/Magento/Catalog/etc/webapi.xml#l36

返回的值将表示\ Magento \ Catalog \ Api \ Data \ ProductInterface(包括其他属性) https://github.com/magento/magento2/blob/develop/app/code/Magento/Catalog/Api/Data/ProductInterface.php

检查\ Magento \ Catalog \ Api \ ProductRepositoryInterface ::获取GET / V1 / products /:sku REST API的服务。

您可以对所有产品SKU提出多个请求。

OR

您可以使用搜索API根据您的条件在单个请求中提取整个列表:

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

在这种情况下,正在搜索带有SKU的产品 - 简单和简单2。

答案 1 :(得分:1)

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();
}

同样,您可以按类别ID更改$ requestUrl并过滤产品列表,并获取产品详细信息。

请确认是否解决了您的问题。否则,我会发布另一个解决方案。

答案 2 :(得分:0)

答案 3 :(得分:0)

请尝试使用此端点而不是端点:

/V1/products?searchCriteria[filter_groups][0][filters][0][field]=category_id&searchCriteria[filter_groups][0][filters][0][value]=24&searchCriteria[filter_groups][0][filters][0][condition_type]=eq

与@Alexander Timonchev相同,但您必须在&之后删除空格