3

我正在开发原生移动应用程序后端是 magento2,我想在客户选择一个类别后显示产品。我可以在休息请求中按类别获取产品列表,但该列表没有关于产品的太多详细信息。

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

(24 是类别 ID)

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

Magento 1.9 的早期产品列表类似于


       {
    2:{
    entity_id:“2”
    type_id:“简单”
    sku:“李维斯背包”
    描述:“背包”
    short_description: "包包"
    元关键字:空
    名称:“李维斯背包”
    元标题:空
    元描述:空
    常规价格含税:45
    常规价格不含税:45
    final_price_with_tax:45
    final_price_without_tax: 45
    is_saleable: 真
    image_url:“http://172.16.8.24:8080/magento/media/catalog/product/cache/0/image/9df78eab33525d08d6e5fb8d27136e95/images/catalog/product/placeholder/image.jpg”
    }

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

4

4 回答 4

5

也许您可以尝试使用 GET /V1/products/:skuREST API 来获取所有详细信息。

关联

返回的值将表示\Magento\Catalog\Api\Data\ProductInterface(包括附加属性)

参考链接

检查\Magento\Catalog\Api\ProductRepositoryInterface::getGET /V1/products/:skuREST API 的哪些服务。

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

或者

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

例如:

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][条件类型]=eq&searchCriteria[current_page]=1&searchCriteria[page_size]=2

对于具有 SKU 的产品 - 正在搜索 simple 和 Simple2。

于 2015-05-06T19:51:55.350 回答
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();
}

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

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

于 2016-07-15T12:40:43.500 回答
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 相同,但您必须删除之后的空格&

于 2018-03-19T02:44:42.253 回答