문제

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 magento.stackexchange
scroll top