Search Criteria not working with Soap API
-
27-12-2020 - |
Вопрос
Description:
I am working with Soap API on Magento 2, I am trying to get a filtered list of all products on my store using the following code.
P.S: I have tried to filter other fields like Sku,Id and Name
Problem:
No matter how I change the search criteria array, It keeps returning the complete list of unfiltered products.
Any Idea on what I am doing wrong?
$wsdlurl = 'http://example.com/index.php/soap/default?wsdl&services=catalogProductRepositoryV1';
$token = 'livutybnnlu6b6bhuvtr86f93p5khgjb';
$opts = ['http' => ['header' => "Authorization: Bearer ".$token]];
$context = stream_context_create($opts);
$soapClient = new \Zend\Soap\Client($wsdlurl);
$soapClient->setSoapVersion(SOAP_1_2);
$soapClient->setStreamContext($context);
$serviceArgs = array('searchCriteria'=>
array('filterGroups' =>
array('filters' =>
array('field' => 'typeId',
'value' => 'downloadable',
)
)
)
);
$result = $soapClient->catalogProductRepositoryV1GetList($serviceArgs );
echo var_dump( $result->result->items);
Решение
You have to write the SOAP API, like below.
<?php
require('vendor/zendframework/zend-server/src/Client.php');
require('vendor/zendframework/zend-soap/src/Client.php');
require('vendor/zendframework/zend-soap/src/Client/Common.php');
$token = 'seguq0wb13s495lip1j3p8ms3iledcnd'; // Use your Access Token
$wsdlUrl = 'http://YourMagentoRoot/soap/default?wsdl&services=catalogProductRepositoryV1';
$opts = ['http'=> ['header' => "Authorization: Bearer " .$token]];
$context = stream_context_create($opts);
$soapClient = new \Zend\Soap\Client($wsdlUrl);
$soapClient->setSoapVersion(SOAP_1_2);
$soapClient->setStreamContext($context);
// get all products or use filters on collection
$searchCriteria = [
'searchCriteria' => [
'filterGroups' => [
[
'filters' => [
[
'field' => 'sku',
'value' => '24-MB01',
'condition_type' => 'eq',
],
],
],
],
],
];
$soapClient = new \Zend\Soap\Client($wsdlUrl);
$soapClient->setSoapVersion(SOAP_1_2);
$soapClient->setStreamContext($context);
$response = $soapClient->catalogProductRepositoryV1GetList($searchCriteria);
var_dump( $response->result->items); ?>
Make sure to use your own access token and update the filter condition as per your requirement. Clear the cache and check the result. This is tested successfully in my local machine.
Не связан с magento.stackexchange