Magento 2 rest API:按名称过滤类别
Magento 2 rest API: filter category by name
我需要使用 Magento 2 rest api 按名称查找类别。
我使用 /all/V1/categories
和过滤器,但无法正常工作。
这是代码:
$url = "https://*******/index.php/rest";
$token_url= $url."/V1/integration/admin/token";
$ch = curl_init();
$data = array("username" => USERNAME, "password" => PASSWORD);
$data_string = json_encode($data);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $token_url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json'
));
$token = curl_exec($ch);
$adminToken= json_decode($token);
$headers = array('Content-Type:application/json','Authorization:Bearer '.$adminToken);
$ch = curl_init();
$apiUrl = $url."/all/V1/categories";
$data = [
"searchCriteria" => [
"filterGroups" => [
"filters" => [
"fieldName" => "category",
"value" => 'CATEGORY NAME',
"condition_type" => 'eq'
]
]
],
];
$data_string = json_encode($data);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
$response = json_decode($response, TRUE);
curl_close($ch);
这是启动代码后出现的错误:
array:3 [▼ "message" => ""%fieldName" is required." "parameters" => array:1 [▼ "fieldName" => "category" ]
您可以通过向端点 rest/all/V1/categories/list
添加搜索条件,使用 Magento 2 rest api 按名称获取类别。对 api 的请求应如下所示:
https://website.com/rest/all/V1/categories/list?searchCriteria%5BfilterGroups%5D%5B0%5D%5Bfilters%5D%5B0%5D%5Bfield%5D=name&searchCriteria%5BfilterGroups%5D%5B0%5D%5Bfilters%5D%5B0%5D%5Bcondition_type%5D=eq&searchCriteria%5BfilterGroups%5D%5B0%5D%5Bfilters%5D%5B0%5D%5Bvalue%5D=category_name
将 website.com
替换为您网站的 url,将 category_name
替换为类别搜索词。
如果搜索字符串还应匹配部分类别名称,您可以使用 like
而不是 eq
。
更多信息请参考:https://devdocs.magento.com/guides/v2.4/rest/performing-searches.html
我需要使用 Magento 2 rest api 按名称查找类别。
我使用 /all/V1/categories
和过滤器,但无法正常工作。
这是代码:
$url = "https://*******/index.php/rest";
$token_url= $url."/V1/integration/admin/token";
$ch = curl_init();
$data = array("username" => USERNAME, "password" => PASSWORD);
$data_string = json_encode($data);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $token_url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json'
));
$token = curl_exec($ch);
$adminToken= json_decode($token);
$headers = array('Content-Type:application/json','Authorization:Bearer '.$adminToken);
$ch = curl_init();
$apiUrl = $url."/all/V1/categories";
$data = [
"searchCriteria" => [
"filterGroups" => [
"filters" => [
"fieldName" => "category",
"value" => 'CATEGORY NAME',
"condition_type" => 'eq'
]
]
],
];
$data_string = json_encode($data);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
$response = json_decode($response, TRUE);
curl_close($ch);
这是启动代码后出现的错误:
array:3 [▼ "message" => ""%fieldName" is required." "parameters" => array:1 [▼ "fieldName" => "category" ]
您可以通过向端点 rest/all/V1/categories/list
添加搜索条件,使用 Magento 2 rest api 按名称获取类别。对 api 的请求应如下所示:
https://website.com/rest/all/V1/categories/list?searchCriteria%5BfilterGroups%5D%5B0%5D%5Bfilters%5D%5B0%5D%5Bfield%5D=name&searchCriteria%5BfilterGroups%5D%5B0%5D%5Bfilters%5D%5B0%5D%5Bcondition_type%5D=eq&searchCriteria%5BfilterGroups%5D%5B0%5D%5Bfilters%5D%5B0%5D%5Bvalue%5D=category_name
将 website.com
替换为您网站的 url,将 category_name
替换为类别搜索词。
如果搜索字符串还应匹配部分类别名称,您可以使用 like
而不是 eq
。
更多信息请参考:https://devdocs.magento.com/guides/v2.4/rest/performing-searches.html