/**
* WPSC get state by id function, gets either state code or state name depending on param
*
* @since 3.7
* $param int $id the id for the region
* @param string $return_value either 'name' or 'code' depending on what you want returned
*/
function wpsc_get_state_by_id($id, $return_value)
{
    $region = new WPSC_Region(WPSC_Countries::get_country_id_by_region_id($id), $id);
    $value = '';
    if ($return_value == 'name') {
        $value = $region->get_name();
    } elseif ($return_value == 'code') {
        $value = $region->get_code();
    }
    return $value;
}
Example #2
0
 function getQuote()
 {
     global $wpdb, $wpec_ash, $wpsc_cart, $wpec_ash_tools;
     // Arguments array for various functions to use
     $args = array();
     $args['dest_ccode'] = wpsc_get_customer_meta('shippingcountry');
     // Get the ups settings from the ups account info page (Shipping tab)
     $wpsc_ups_settings = get_option('wpsc_ups_settings', array());
     //Disable International Shipping. Default: Enabled, as it currently is.
     $args['intl_rate'] = isset($wpsc_ups_settings['intl_rate']) && !empty($wpsc_ups_settings['intl_rate']) ? FALSE : TRUE;
     if (!$args['intl_rate'] && $args['dest_ccode'] != get_option('base_country')) {
         return array();
     }
     // Destination zip code
     $args['dest_pcode'] = (string) wpsc_get_customer_meta('shippingpostcode');
     if (!is_object($wpec_ash_tools)) {
         $wpec_ash_tools = new ASHTools();
     }
     if (empty($args['dest_pcode']) && $wpec_ash_tools->needs_post_code($args['dest_ccode'])) {
         // We cannot get a quote without a zip code so might as well return!
         return array();
     }
     // Get the total weight from the shopping cart
     $args['weight'] = wpsc_cart_weight_total();
     if (empty($args['weight'])) {
         return array();
     }
     $args['dest_state'] = '';
     $wpsc_country = new WPSC_Country(wpsc_get_customer_meta('shippingcountry'));
     if ($wpsc_country->has_regions()) {
         $wpsc_region = $wpsc_country->get_region(wpsc_get_customer_meta('shippingregion'));
         if (is_a($wpsc_region, 'WPSC_Region')) {
             $args['dest_state'] = $wpsc_region->get_code();
         }
     }
     if (empty($args['dest_state'])) {
         $args['dest_state'] = wpsc_get_customer_meta('shippingstate');
     }
     if (!is_object($wpec_ash)) {
         $wpec_ash = new ASH();
     }
     $shipping_cache_check['state'] = $args['dest_state'];
     //The destination is needed for cached shipment check.
     $shipping_cache_check['country'] = $args['dest_ccode'];
     $shipping_cache_check['zipcode'] = $args['dest_pcode'];
     $this->shipment = $wpec_ash->get_shipment();
     $this->shipment->set_destination($this->internal_name, $shipping_cache_check);
     //Set this shipment's destination.
     $this->shipment->rates_expire = date('Y-m-d');
     $args['shipper'] = $this->internal_name;
     $args['singular_shipping'] = array_key_exists('singular_shipping', $wpsc_ups_settings) ? $wpsc_ups_settings['singular_shipping'] : '0';
     if ($args['weight'] > 150 && !(bool) $args['singular_shipping']) {
         // This is where shipping breaks out of UPS if weight is higher than 150 LBS
         $over_weight_txt = apply_filters('wpsc_shipment_over_weight', __('Your order exceeds the standard shipping weight limit. Please contact us to quote other shipping alternatives.', 'wpsc'), $args);
         $shipping_quotes[$over_weight_txt] = 0;
         // yes, a constant.
         $wpec_ash->cache_results($this->internal_name, array($shipping_quotes), $this->shipment);
         //Update shipment cache.
         return array($shipping_quotes);
     }
     $cache = $wpec_ash->check_cache($this->internal_name, $this->shipment);
     //And now, we're ready to check cache.
     // We do not want to spam UPS (and slow down our process) if we already
     // have a shipping quote!
     if (count($cache['rate_table']) >= 1) {
         return $cache['rate_table'];
     }
     // Final rate table
     $rate_table = array();
     // API Auth settings //
     $args['username'] = array_key_exists('upsaccount', $wpsc_ups_settings) ? $wpsc_ups_settings['upsusername'] : '';
     $args['password'] = array_key_exists('upspassword', $wpsc_ups_settings) ? $wpsc_ups_settings['upspassword'] : '';
     $args['api_id'] = array_key_exists('upsid', $wpsc_ups_settings) ? $wpsc_ups_settings['upsid'] : '';
     $args['account_number'] = array_key_exists('upsaccount', $wpsc_ups_settings) ? $wpsc_ups_settings['upsaccount'] : '';
     $args['negotiated_rates'] = array_key_exists('ups_negotiated_rates', $wpsc_ups_settings) ? $wpsc_ups_settings['ups_negotiated_rates'] : '';
     $args['residential'] = $wpsc_ups_settings['49_residential'];
     $args['insured_shipment'] = array_key_exists('insured_shipment', $wpsc_ups_settings) ? $wpsc_ups_settings['insured_shipment'] : '0';
     // What kind of pickup service do you use ?
     $args['DropoffType'] = $wpsc_ups_settings['DropoffType'];
     $args['packaging'] = $wpsc_ups_settings['48_container'];
     // Preferred Currency to display
     $currency_data = WPSC_Countries::get_currency_code(get_option('currency_type'));
     if (!empty($currency_data)) {
         $args['currency'] = $currency_data;
     } else {
         $args['currency'] = 'USD';
     }
     // Shipping billing / account address
     $region = new WPSC_Region(get_option('base_country'), get_option('base_region'));
     $args['shipr_state'] = $region->get_code();
     $args['shipr_city'] = get_option('base_city');
     $args['shipr_ccode'] = get_option('base_country');
     $args['shipr_pcode'] = get_option('base_zipcode');
     // Physical Shipping address being shipped from
     $args['shipf_state'] = $args['shipr_state'];
     $args['shipf_city'] = $args['shipr_city'];
     $args['shipf_ccode'] = $args['shipr_ccode'];
     $args['shipf_pcode'] = $args['shipr_pcode'];
     $args['units'] = 'LBS';
     $args['cart_total'] = $wpsc_cart->calculate_subtotal(true);
     $args = apply_filters('wpsc_shipment_data', $args, $this->shipment);
     if (isset($args['stop'])) {
         //Do not get rates.
         return array();
     }
     // Build the XML request
     $request = $this->_buildRateRequest($args);
     // Now that we have the message to send ... Send it!
     $raw_quote = $this->_makeRateRequest($request);
     // Now we have the UPS response .. unfortunately its not ready
     // to be viewed by normal humans ...
     $quotes = $this->_parseQuote($raw_quote);
     // If we actually have rates back from UPS we can use em!
     if ($quotes != false) {
         $rate_table = apply_filters('wpsc_rates_table', $this->_formatTable($quotes, $args['currency']), $args, $this->shipment);
     } else {
         if (isset($wpsc_ups_settings['upsenvironment'])) {
             echo '<strong>:: GetQuote ::DEBUG OUTPUT::</strong><br />';
             echo 'Arguments sent to UPS';
             print_r($args);
             echo '<hr />';
             print $request;
             echo '<hr />';
             echo 'Response from UPS';
             echo $raw_quote;
             echo '</strong>:: GetQuote ::End DEBUG OUTPUT::';
         }
     }
     $wpec_ash->cache_results($this->internal_name, $rate_table, $this->shipment);
     // return the final formatted array !
     return $rate_table;
 }