davidtsadler / ebay-sdk-php

An eBay SDK for PHP. Use the eBay API in your PHP projects.
Apache License 2.0
349 stars 341 forks source link

GetCategorySpecifics not response specifics. #122

Closed slaveykov closed 7 years ago

slaveykov commented 7 years ago

Hello, i try to use category specifics but not response anything about specifics.

public function getCategorySpecifics($token, $categoryId, $siteId=0)
{
    $service = new \DTS\eBaySDK\Trading\Services\TradingService([
        'credentials'=>[
          'appId'=>$this->appData['EBAY_APPNAME'],
          'certId'=>$this->appData['EBAY_CERTNAME'],
          'devId'=>$this->appData['EBAY_DEVNAME']
        ],
        'sandbox'=>$this->appData['EBAY_SANDBOX'],
        'siteId'=> $siteId
    ]);

    $request = new \DTS\eBaySDK\Trading\Types\GetCategorySpecificsRequestType();

    $request->RequesterCredentials = new \DTS\eBaySDK\Trading\Types\CustomSecurityHeaderType();
    $request->RequesterCredentials->eBayAuthToken = $token;

    $request->CategoryID[]= "".$categoryId;
    $response = $service->GetCategorySpecifics($request);
    $response = $response->toArray();

    $specifications = [];
    $i=0;

    if(isset($response['Recommendations'][0]['NameRecommendation'])) {
      foreach($response['Recommendations'][0]['NameRecommendation'] as $recommend) {
        $specifications[$i]['name'] = $recommend['Name'];
        if(!isset($recommend['ValueRecommendation'][0]['Value'])){
          $recommend['ValueRecommendation'][0]['Value'] = '';
        }
        $specifications[$i]['value'] = '';//$recommend['ValueRecommendation'][0]['Value'];
        $i++;
      }
    }

    return $specifications;
}
davidtsadler commented 7 years ago
$response = $service->GetCategorySpecifics($request);
$specifications = [];

if (isset($response->Recommendations)) {
   foreach($response->Recommendations[0]->NameRecommendation as $specific) {
       $values = [];
       foreach($specific->ValueRecommendation as $value) {
           $values[] = $value->Value;
       }
       $specifications[] = [
           'name'   => $specific->Name,
           'values' => $values
       ];
   }
}

The above code should provide an array of item specifics like the example below.

[
    [
        'name'  => 'Color',
        'values' => ['Red', 'White', 'Blue']
    ],
    [
        'name'  => 'Size',
        'values' => ['S', 'M', 'L']
    ]
]

As a bonus the SDK supports JMESPath expressions. The below code will also produce the same array.

$response = $service->GetCategorySpecifics($request);
$specifications = $response->search('Recommendations[].NameRecommendation[].{name:Name, values: ValueRecommendation[].Value}');