Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
breakoutModefull-width
languagephp
<?php

require_once __DIR__ . '/vendor/autoload.php';

// Configure API key authorization: ultraCartSimpleApiKey
ultracart\v2\Configuration::getDefaultConfiguration()->setApiKey('x-ultracart-simple-key', 'YOUR API KEY HERE');

// 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]);

$coupon_api = new ultracart\v2\Apiapi\CouponApi(
    $client,
    ultracart\v2\Configuration::getDefaultConfiguration(),
    new ultracart\v2\HeaderSelector("2016-10-01")
);

$_limit = 100; // 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_code"; // string | The sort order of the orders.  See Sorting documentation for examples of using multiple values and sorting by ascending and descending.
$_expand = "all"; // string | The object expansion to perform on the result.

$coupon_query = new \ultracart\v2\models\CouponQuery();

$coupon_map = array();

for ($_offset = 0; ; $_offset += $_limit) {
    try {
        $result = $coupon_api->getCouponsByQuery($coupon_query, $_limit, $_offset, $_sort, $_expand);

        if ($result->getSuccess()) {
            for ($i = 0; $i < count($result->getCoupons()); $i++) {

                $coupon = $result->getCoupons()[$i];


                $coupon_map[strtoupper($coupon->getMerchantCode())] = $coupon;
            }

            // Are we done?
            if (count($result->getCoupons()) < $_limit) {
                break;
            }
        } else {
            echo 'Failed to retrieve coupons' . PHP_EOL;
            var_dump($result);
            break;
        }
    } catch (Exception $e) {
        echo 'Exception when calling CouponApi->getCoupons: ', $e->getMessage(), PHP_EOL;
    }
}

echo 'Created map of ' . count($coupon_map) . ' coupons.' . PHP_EOL;

$order_api = new ultracart\v2\Api\OrderApi(
    $client,
    ultracart\v2\Configuration::getDefaultConfiguration(),
    new ultracart\v2\HeaderSelector("2016-10-01")
);
$order_query = new \ultracart\v2\models\OrderQuery();
$order_query->setCreationDateBegin("2019-01-01T00:00:00-04:00"); // TODO: Adjust for desired date range
$order_query->setCreationDateEnd("2020-01-01T00:00:00-04:00");

$_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 = "order_id"; // string | The sort order of the orders.  See Sorting documentation for examples of using multiple values and sorting by ascending and descending.
$_expand = "billing,coupon,item,shipping,summary"; // string | The object expansion to perform on the result.

for ($_offset = 0; ; $_offset += $_limit) {
//    echo 'Pulling offset ' . $_offset . PHP_EOL;
    try {
        $result = $order_api->getOrdersByQuery($order_query, $_limit, $_offset, $_sort, $_expand);

        if ($result->getSuccess()) {
//            echo 'Retrieved ' . count($result->getOrders()) . PHP_EOL;

            for ($i = 0; $i < count($result->getOrders()); $i++) {

                $order = $result->getOrders()[$i];

                // Does the order have any coupons
                if ($order->getCoupons() != null && count($order->getCoupons()) > 0) {
                    echo 'Order: ' . $order->getOrderId() . ' has coupons.' . PHP_EOL;
                    // Log out discounts at the item level
                    for ($j = 0; $j < count($order->getItems()); $j++) {
                        $item = $order->getItems()[$j];

                        if ($item->getDiscount() != null && $item->getDiscount()->getValue() > 0) {
                            echo '  Item: ' . $item->getMerchantItemId() . ' has a discount of ' . $item->getDiscount()->getValue() . ' on qty of ' . $item->getDiscountQuantity() . PHP_EOL;
                        }
                    }
                    // Log out discounts at the subtotal level
                    if ($order->getSummary()->getSubtotalDiscount()->getValue() > 0) {
                        echo '  Subtotal discount: ' . $order->getSummary()->getSubtotalDiscount()->getValue() . PHP_EOL;
                    }
                    if ($order->getSummary()->getShippingHandlingTotalDiscount()->getValue() > 0) {
                        echo '  Shipping discount: ' . $order->getSummary()->getShippingHandlingTotalDiscount()->getValue() . PHP_EOL;
                    }


                    for ($j = 0; $j < count($order->getCoupons()); $j++) {
                        $order_coupon = $order->getCoupons()[$j];

                        if (array_key_exists(strtoupper($order_coupon->getBaseCouponCode()), $coupon_map)) {
                            $coupon = $coupon_map[strtoupper($order_coupon->getBaseCouponCode())];

                            echo '  Coupon: ' . $coupon->getMerchantCode() . ' - ' . $coupon->getCalculatedDescription() . PHP_EOL;

                            // TODO: Do whatever you want with the full coupon object
                        } else {
                            // This indicates that the coupon has been deleted since this order was placed
                            echo 'Cant find base coupon object for ' . $order_coupon->getBaseCouponCode() . PHP_EOL;
                        }
                    }
                }
            }

            // Are we done?
            if (count($result->getOrders()) < $_limit) {
                echo 'Finished' . PHP_EOL;
                break;
            }
        } else {
            echo 'Failed to retrieve orders' . PHP_EOL;
            var_dump($result);
            break;
        }
    } catch (Exception $e) {
        echo 'Exception when calling OrderApi->getOrdersByQuery: ', $e->getMessage(), PHP_EOL;
    }
}


?>