PHP SDK Sample: Update Avalara Item Settings

This sample will populate the item properties associated with Avalara.

 

<?php require_once __DIR__ . '/vendor/autoload.php'; // Disable SSL verification if we're having issues with this PHP install not having the proper CA installed. Fix your CA for a production environment! // Set debug to true if you need more information on request/response $client = new GuzzleHttp\Client(['verify' => false, 'debug' => false]); // Configure API key authorization: ultraCartSimpleApiKey $config = new ultracart\v2\Configuration(); $config->setApiKey('x-ultracart-simple-key', 'YOUR API KEY HERE'); $api_instance = new ultracart\v2\api\ItemApi( $client, $config, new ultracart\v2\HeaderSelector("2016-10-01") ); $_expand = "properties"; // string | The object expansion to perform on the result. // Fetch the item $item_response = $api_instance->getItemByMerchantItemId("SAMPLE", $_expand); if ($item_response->getSuccess()) { $item = $item_response->getItem(); // Init the array if it's null because no properties exist if ($item->getProperties() == null) { $item->setProperties(array()); } $newTaxCodeDescription = "CODEDESC"; // TODO: Set this to the value you want $newTaxCode = "CODE"; // TODO: Set this to the value you want $addTaxCodeDescription = true; $addTaxCode = true; // Go through and look for an existing property we can change. $properties = $item->getProperties(); for ($j = 0; $j < count($properties); $j++) { $property = $properties[$j]; if ($property->getName() == "avalaraTaxCodeDescription") { $property->setValue($newTaxCodeDescription); $addTaxCodeDescription = false; } if ($property->getName() == "avalaraTaxCode") { $property->setValue($newTaxCode); $addTaxCode = false; } } // Add new ones if necessary if ($addTaxCodeDescription) { $property = new ultracart\v2\models\ItemProperty(); $property->setName("avalaraTaxCodeDescription"); $property->setValue($newTaxCodeDescription); array_push($properties, $property); } if ($addTaxCode) { $property = new ultracart\v2\models\ItemProperty(); $property->setName("avalaraTaxCode"); $property->setValue($newTaxCode); array_push($properties, $property); } $item->setProperties($properties); $api_instance->updateItem($item, $item->getMerchantItemOid(), $_expand); echo 'Updated item' . PHP_EOL; } echo 'DONE' . PHP_EOL; ?>