1

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

1 Answer 1

3

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.

6
  • Thank you for you answer. Actually, The condition type is optional, it has a default value = 'eq' (Check vendor/magento/framework/api/filter.php line75 )
    – Siba Al
    Jan 23, 2018 at 15:09
  • ok, so even if adding the condition_type, you are getting the same result ?
    – MGento
    Jan 23, 2018 at 17:40
  • Then , it could be due to the format of the search criteria. Try the updated syntax in my answer.
    – MGento
    Jan 23, 2018 at 18:15
  • Didn't work either..
    – Siba Al
    Jan 25, 2018 at 18:18
  • I have updated my answer and I made sure it works :) Please have a try
    – MGento
    Jan 29, 2018 at 17:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.