jlevers / selling-partner-api

A PHP client library for Amazon's Selling Partner API
BSD 3-Clause "New" or "Revised" License
409 stars 200 forks source link

Update country_of_origin using Patch request on listing items api #821

Open greenhof opened 6 days ago

greenhof commented 6 days ago

Problem description:

I am banging my head on replacing country_of_origin attribute on multiple listings. Could you please take a look at my code and point me in the right direction?

Error:

"errors": [
    {
      "code": "InvalidInput",
      "message": "Invalid empty value provided in patch at index of 0.",
      "details": ""
    }
  ]

Code

$data = new ListingsItemPatchRequest(
          $productType,
          [
              new PatchOperation(
                  'replace',
                  '/attributes/country_of_origin',
                  [

                          'value' => $cocs[$sku],
                          'marketplace_id' => $mplaceKurz[$code],

                  ]

              ),
          ]

      );
 $patchresult = $listingsItemsApi->patchListingsItem('A2XXXXXXX',$sku, $data, array($mplaceKurz[$code]));

here is the generated request body:

SellingPartnerApi\Seller\ListingsItemsV20210801\Dto\ListingsItemPatchRequest Object ( [productType] => BEAUTY [patches] => Array ( [0] => SellingPartnerApi\Seller\ListingsItemsV20210801\Dto\PatchOperation Object ( [op] => replace [path] => /attributes/country_of_origin [value] => Array ( [value] => DE [marketplace_id] => AMEN7PMS3EDWL )

            )

    )

)

jlevers commented 4 days ago

The value typically has to be a nested array, because there are some attributes that can have multiple values. Give this a shot:

$data = new ListingsItemPatchRequest(
    $productType,
    [
        new PatchOperation(
                'replace',
                '/attributes/country_of_origin',
                [[
                    'value' => $cocs[$sku],
                    'marketplace_id' => $mplaceKurz[$code],
                ]]
            ),
        ]
);
$patchresult = $listingsItemsApi->patchListingsItem('A2XXXXXXX', $sku, $data, [$mplaceKurz[$code]]);