PHP SDK Sample: Adding Package Requirements to Perishable Items
This tutorial loads all the items from an account and assigns package requirements whenever the item is perishable. This utility was used to configure a large amount of perishable items to packages that have integrated coolers in them.
<?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")
);
$_limit = 200; // int | The maximum number of records to return on this one API call. (Maximum 200)
$_offset = 0; // int | Pagination of the record set. Offset is a zero based index.
$_sort = "merchant_item_id"; // string | The sort order of the orders. See Sorting documentation for examples of using multiple values and sorting by ascending and descending.
$_expand = "shipping,shipping.package_requirements"; // string | The object expansion to perform on the result.
for ($_offset = 0; ; $_offset += $_limit) {
echo 'Pulling offset ' . $_offset . PHP_EOL;
try {
$result = $api_instance->getItems(null, null, $_limit, $_offset, null, null, $_expand, false);
if ($result->getSuccess()) {
echo 'Retrieved ' . count($result->getItems()) . PHP_EOL;
for ($i = 0; $i < count($result->getItems()); $i++) {
$item = $result->getItems()[$i];
// Is this item perisahble?
if ($item->getShipping() != null && $item->getShipping()->getPerishableClassName() != null) {
echo $item->getMerchantItemId() . PHP_EOL;
if ($item->getShipping()->getPackageRequirements() == null) {
echo " adding package requirements";
$package_requirements = array();
// TODO: Adjust these to be your desired packages
$package = new \ultracart\v2\models\ItemShippingPackageRequirement();
$package->setPackageName("baby ice chest");
array_push($package_requirements, $package);
$package = new \ultracart\v2\models\ItemShippingPackageRequirement();
$package->setPackageName("small ice chest");
array_push($package_requirements, $package);
$package = new \ultracart\v2\models\ItemShippingPackageRequirement();
$package->setPackageName("medium ice chest");
array_push($package_requirements, $package);
$package = new \ultracart\v2\models\ItemShippingPackageRequirement();
$package->setPackageName("large ice chest");
array_push($package_requirements, $package);
$package = new \ultracart\v2\models\ItemShippingPackageRequirement();
$package->setPackageName("xlarge ice chest");
array_push($package_requirements, $package);
$item->getShipping()->setPackageRequirements($package_requirements);
$item_response = $api_instance->updateItem($item, $item->getMerchantItemOid(), $_expand);
$item = $item_response->getItem();
var_dump($item->getShipping()->getPackageRequirements());
} else {
echo " existing package requirements:";
var_dump($item->getShipping()->getPackageRequirements());
}
}
}
// Are we done?
if (count($result->getItems()) < $_limit) {
echo 'Finished' . PHP_EOL;
break;
}
} else {
echo 'Failed to retrieve items' . PHP_EOL;
var_dump($result);
break;
}
} catch (Exception $e) {
echo 'Exception when calling ItemApi->getItems: ', $e->getMessage(), PHP_EOL;
}
}
?>