如何在RESTAPI后端是magento2中获取产品列表及其详细信息
我正在使用本机移动应用程序后端是magento2,我想在客户选择类别后显示产品。我可以在rest请求中按类别获得产品列表,但该列表没有关于该产品的详细信息 请求: (24是类别ID) 答复:[{“sku”:“WH01”,“位置”:1,“类别id”:“24”},…] 在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" } { 2: { 实体标识:“2” 类型_id:“简单” sku:“利维斯背包” 描述:“背包” 简短描述:“背包” meta_关键字:null 名称:“Levis Bagpack” 元标题:空 元描述:空 含税正常价格:45 正常价格(不含税):45 含税最终价格:45 无税最终价格:45 _是否可销售:正确 图像\u url:“http://172.16.8.24:8080/magento/media/catalog/product/cache/0/image/9df78eab33525d08d6e5fb8d27136e95/images/catalog/product/placeholder/image.jpg" }如何在RESTAPI后端是magento2中获取产品列表及其详细信息,rest,magento,magento2,Rest,Magento,Magento2,我正在使用本机移动应用程序后端是magento2,我想在客户选择类别后显示产品。我可以在rest请求中按类别获得产品列表,但该列表没有关于该产品的详细信息 请求: (24是类别ID) 答复:[{“sku”:“WH01”,“位置”:1,“类别id”:“24”},…] 在Magento 1.9的早期产品列表中 { 2: { entity_id: "2" type_id: "simple" sku: "Levis Bagpack" description: "B
我应该做些什么来获取更多关于产品的信息,以便在移动应用程序中显示图像和其他内容?也许您可以尝试使用get
/V1/products/:sku
REST API来获取所有详细信息
返回值将表示\Magento\Catalog\Api\Data\ProductInterface
(包括附加属性)
检查为get/V1/products/:sku
REST Api提供服务的\Magento\Catalog\Api\ProductRepositoryInterface::get
您可以对所有产品SKU发出多个请求
或
您可以使用搜索API根据您的条件在单个请求中获取整个列表:
例如:
对于带有SKU的产品,正在搜索simple和Simple2
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筛选产品列表,并获取产品详细信息
请确认它是否解决了您的问题。否则我将发布另一个解决方案。您可以尝试此方法,其中“30”是类别id。请尝试使用此终结点而不是您的终结点:
/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相同,但在
&
找到解决方案后,您必须删除空格?@airboss-您的回答建议如何获取此类产品的详细信息,但问题是如何使用类别id获取产品列表。问题非常简单,但您的回答非常复杂。