Example #1
0
 public function getUSPSRates($id_zone, $is_cart, $cart, $product_weight, $dest_zip, $dest_state, $dest_country, $currency, $product, $id_product_attribute, $qty)
 {
     $rates = array();
     if (file_exists(dirname(__FILE__) . '/../../usps/classes/RateAvailableServices.php')) {
         include_once dirname(__FILE__) . '/../../usps/usps.php';
         include_once dirname(__FILE__) . '/../../usps/classes/RateAvailableServices.php';
         $usps = new USPS();
         if ($usps->active) {
             // Get handling cost (once)
             $handling = Configuration::get('PS_SHIPPING_HANDLING');
             $carriers = $usps->getCarriers($id_zone, $cart, $is_cart, $product);
             foreach ($carriers as $carrier) {
                 //check if USPS module is new or old
                 if (get_parent_class($usps) == 'PrestoChangeoCarrierModule') {
                     $USPSRate = new USPSRate();
                     $amount = $USPSRate->getRate($carrier['id_carrier'], $carrier['id_zone'], $is_cart != '' ? $cart->getTotalWeight() : max($product_weight, 0.001), $dest_zip, $dest_state, $dest_country, "", $is_cart != '' ? 0 : $product->getPrice(true, $id_product_attribute, 6, NULL, false, true, $qty), $is_cart != '' ? 0 : $product->id, $is_cart != '' ? 0 : $id_product_attribute, $qty);
                 } else {
                     $amount = getUSPSRate($carrier['id_carrier'], $carrier['id_zone'], $is_cart != '' ? $cart->getTotalWeight() : max($product_weight, 0.001), $dest_zip, $dest_state, $dest_country, "", $is_cart != '' ? 0 : $product->getPrice(true, $id_product_attribute, 6, NULL, false, true, $qty), $is_cart != '' ? 0 : $product->id, $is_cart != '' ? 0 : $id_product_attribute, $qty);
                 }
                 // Add product spercific cost + handling fee
                 if ($amount > 0) {
                     $amount += $this->getExtraShippingCost($carrier, $handling, $is_cart != '' ? $cart->getProducts() : array(), $product, $qty);
                 }
                 // Apply shipping tax if needed
                 if (!Tax::excludeTaxeOption()) {
                     $carrierTax = Tax::getCarrierTaxRate($carrier['id_carrier']);
                 }
                 if (isset($carrierTax) && $amount !== false) {
                     $amount *= 1 + $carrierTax / 100;
                 }
                 $amount = $amount === false ? -1 : Tools::convertPrice($amount, $currency);
                 if ($amount > 0) {
                     $rates[$carrier['name']] = array(Tools::displayPrice($amount, $currency, false), $carrier['id_carrier']);
                 } elseif ($amount !== false && $amount == 0) {
                     $rates[$carrier['name']] = array($usps->l('Free!'), $carrier['id_carrier']);
                 }
             }
         }
     }
     return $rates;
 }
<?php

// Load the class
require_once '../USPSRate.php';
// Initiate and set the username provided from usps
$rate = new USPSRate('xxxx');
// During test mode this seems not to always work as expected
//$rate->setTestMode(true);
// Create new package object and assign the properties
// apartently the order you assign them is important so make sure
// to set them as the example below
// set the USPSRatePackage for more info about the constants
$package = new USPSRatePackage();
$package->setService(USPSRatePackage::SERVICE_FIRST_CLASS);
$package->setFirstClassMailType(USPSRatePackage::MAIL_TYPE_LETTER);
$package->setZipOrigination(91601);
$package->setZipDestination(91730);
$package->setPounds(0);
$package->setOunces(3.5);
$package->setContainer('');
$package->setSize(USPSRatePackage::SIZE_REGULAR);
$package->setField('Machinable', true);
// add the package to the rate stack
$rate->addPackage($package);
// Perform the request and print out the result
print_r($rate->getRate());
print_r($rate->getArrayResponse());
// Was the call successful
if ($rate->isSuccess()) {
    echo 'Done';
} else {