Esempio n. 1
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;
 }
 function test_has_regions()
 {
     $country = new WPSC_Country(self::COUNTRY_ID_WITHOUT_REGIONS);
     $has_regions = $country->has_regions();
     $this->assertFalse($has_regions);
     $country = new WPSC_Country(self::COUNTRY_ID_WITH_REGIONS);
     // USA
     $has_regions = $country->has_regions();
     $this->assertTrue($has_regions);
 }
Esempio n. 3
0
 /**
  * get_tax_rate method, gets the tax rate as a percentage, based on the selected country and region
  * * EDIT: Replaced with WPEC Taxes - this function should probably be deprecated
  * Note: to refresh cart items use wpsc_refresh_cart_items
  *
  * @access public
  */
 function get_tax_rate()
 {
     $country = new WPSC_Country(get_option('base_country'));
     $country_data = WPSC_Countries::get_country(get_option('base_country'), true);
     $add_tax = false;
     if ($this->selected_country == get_option('base_country')) {
         // Tax rules for various countries go here, if your countries tax rules
         // deviate from this, please supply code to add your region
         switch ($this->selected_country) {
             case 'US':
                 // USA!
                 $tax_region = get_option('base_region');
                 if ($this->selected_region == get_option('base_region') && get_option('lock_tax_to_shipping') != '1') {
                     // if they in the state, they pay tax
                     $add_tax = true;
                 } else {
                     if ($this->delivery_region == get_option('base_region')) {
                         // if they live outside the state, but are delivering to within the state, they pay tax also
                         $add_tax = true;
                     }
                 }
                 break;
             case 'CA':
                 // Canada! apparently in canada, the region that you are in is used for tax purposes
                 if ($this->selected_region != null) {
                     $tax_region = $this->selected_region;
                 } else {
                     $tax_region = get_option('base_region');
                 }
                 $add_tax = true;
                 break;
             default:
                 // Everywhere else!
                 $tax_region = get_option('base_region');
                 if ($country->has_regions()) {
                     if (get_option('base_region') == $region) {
                         $add_tax = true;
                     }
                 } else {
                     $add_tax = true;
                 }
                 break;
         }
     }
     if ($add_tax == true) {
         if ($country->has_regions()) {
             $region = $country->get_region($tax_region);
             $tax_percentage = $region->get_tax();
         } else {
             $tax_percentage = $country->get_tax();
         }
     } else {
         // no tax charged = tax equal to 0%
         $tax_percentage = 0;
     }
     if ($this->tax_percentage != $tax_percentage) {
         $this->clear_cache();
         $this->tax_percentage = $tax_percentage;
         $this->wpsc_refresh_cart_items();
     }
 }
Esempio n. 4
0
 /**
  * validate_forms method, validates the input from the checkout page
  * @access public
  */
 function save_forms_to_db($purchase_id)
 {
     foreach ($this->checkout_items as $form_data) {
         if ($form_data->type == 'heading') {
             continue;
         }
         $customer_meta_key = !empty($form_data->unique_name) ? $form_data->unique_name : sanitize_title($form_data->name) . '_' . $form_data->id;
         $checkout_item_values = wpsc_get_customer_meta($customer_meta_key);
         // Prior to release 3.8.14 the billingstate and shippingstate checkout items were used
         // differently depending on if the billingcountry and shippingcountry values contained countries
         // that used regions.  When countries with regions were present, the billing state field was
         // set to the numeric region id, rather than the string name of the region.  A better long term
         // solution may be to have a distinct checkout item to hold the billingregion or shippingregion
         // code when available.
         if ($customer_meta_key == 'billingstate') {
             $current_country = wpsc_get_customer_meta('billingcountry');
             if (!empty($current_country)) {
                 $wpsc_country = new WPSC_Country($current_country);
                 if ($wpsc_country->has_regions()) {
                     $region = wpsc_get_customer_meta('billingregion');
                     if (!empty($region)) {
                         $checkout_item_values = $region;
                     }
                 }
             }
         } elseif ($customer_meta_key == 'shippingstate') {
             $current_country = wpsc_get_customer_meta('shippingcountry');
             if (!empty($current_country)) {
                 $wpsc_country = new WPSC_Country($current_country);
                 if ($wpsc_country->has_regions()) {
                     $region = wpsc_get_customer_meta('shippingregion');
                     if (!empty($region)) {
                         $checkout_item_values = $region;
                     }
                 }
             }
         }
         if (!is_array($checkout_item_values)) {
             $checkout_item_values = array($checkout_item_values);
         }
         global $wpdb;
         foreach ($checkout_item_values as $checkout_item_value) {
             $prepared_query = $wpdb->insert(WPSC_TABLE_SUBMITTED_FORM_DATA, array('log_id' => $purchase_id, 'form_id' => $form_data->id, 'value' => $checkout_item_value), array('%d', '%d', '%s'));
         }
     }
 }
/**
 * Update any values dependant on billing country
 *
 * @since 3.8.14
 *
 * @access private
 * @param mixed $meta_value Optional. Metadata value.
 * @param string $meta_key Metadata name.
 * @param int $visitor_id visitor ID
 * @return none
*/
function _wpsc_updated_visitor_meta_billingcountry($meta_value, $meta_key, $visitor_id)
{
    $old_billing_state = wpsc_get_visitor_meta($visitor_id, 'billingstate', true);
    $old_billing_region = wpsc_get_visitor_meta($visitor_id, 'billingregion', true);
    if (!empty($meta_value)) {
        // check the current state and region values, if either isn't valid for the new country delete them
        $wpsc_country = new WPSC_Country($meta_value);
        if (!empty($old_billing_state) && $wpsc_country->has_regions() && !$wpsc_country->has_region($old_billing_state)) {
            wpsc_delete_visitor_meta($visitor_id, 'billingstate');
        }
        if (!empty($old_billing_region) && $wpsc_country->has_regions() && !$wpsc_country->has_region($old_billing_region)) {
            wpsc_delete_visitor_meta($visitor_id, 'billingregion');
        }
    } else {
        wpsc_delete_visitor_meta($visitor_id, 'billingstate');
        wpsc_delete_visitor_meta($visitor_id, 'billingregion');
    }
}
function _wpsc_filter_validation_rule_state_of($error, $value, $field, $props, $matched_field, $matched_value, $matched_props)
{
    global $wpdb;
    if ($value == '') {
        return $error;
    }
    $country_code = $_POST['wpsc_checkout_details'][$matched_field];
    $country = new WPSC_Country($country_code);
    if (!$country->has_regions()) {
        return $error;
    }
    // state should have been converted into a numeric value already
    // if not, it's an invalid state
    if (!is_numeric($value)) {
        $message = apply_filters('wpsc_validation_rule_invalid_state_message', __('%1$s is not a valid state or province in %2$s', 'wpsc'));
        $message = sprintf($message, $value, $country->get_name());
        $error->add($field, $message, array('value' => $value, 'props' => $props));
        return $error;
    }
    $sql = $wpdb->prepare('SELECT COUNT(id) FROM ' . WPSC_TABLE_REGION_TAX . ' WHERE id = %d', $value);
    $count = $wpdb->get_var($sql);
    if ($count == 0) {
        $message = apply_filters('wpsc_validation_rule_invalid_state_id_message', __('You specified or were assigned an invalid state or province. Please contact administrator for assistance', 'wpsc'));
        $error->add($field, $message, array('value' => $value, 'props' => $props));
    }
    return $error;
}