PHP SDK Sample: Add Item to Cart (Checkout)

This tutorial will demonstrate how to load a cart based upon a cart id and add another item to the cart.

Sample Code
<?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'); // TODO: configure your API key here

$api_instance = new ultracart\v2\api\CheckoutApi(
    $client,
    $config,
    new ultracart\v2\HeaderSelector("2016-10-01")
);

// TODO: Get your cart id from the cookie on the request submitted to your page.
$cart_id = "ABC0BF369C4CE8016D4EB8C6D0409900";

// Obtain the cart with all the item details expanded.  If you need to adjust your expansion on the cart make sure
// to reference the expansion constants located in the documentation: https://www.ultracart.com/api/#resource_checkout.html
$_expand = "items,items.attributes,items.multimedia,items.physical";
$cart_response = $api_instance->getCartByCartId($cart_id, $_expand);

$cart = $cart_response->getCart();

// Get the items array on the cart
$items = $cart->getItems();
// If null, go ahead and initialize it to an empty array
if ($items == null) {
    $items = array();
}

// Create a new item
$item = new ultracart\v2\models\CartItem();
$item->setItemId("BASEBALL"); // TODO: Adjust the item id
$item->setQuantity(1); // TODO: Adjust the quantity
// Stub out the empty options array.
// TODO: If you item has options then you need to create a new ultracart\v2\models\CartItemOption object and push it into the array.
$options = array();
$item->setOptions($options);

// Add the item to the $items array
array_push($items, $item);

// Make sure to update the $cart with the new array
$cart->setItems($items);

// Push the cart up to save the item
$cart_response = $api_instance->updateCart($cart, $_expand);
// Extract the updated cart from the response
$cart = $cart_response->getCart();

// Dump the cart to show the new item added
var_dump($cart);

?>