/** * constructor for a region object * * If null is passed for parameters an empty region is created * * @access public * * @since 3.8.14 * * @param int|string|null required $country The country identifier, can be the string ISO code, * or the numeric wpec country id * * @param int|string|null|array required $region The region identifier, can be the text region code, * or the numeric region id, if an array is passed a * new region will be created and saved in the permanent * data store */ public function __construct($country, $region) { // if a country id or code is passed make sure we have a valid country_id $country_id = $country ? WPSC_Countries::get_country_id($country) : 0; // if we are creating a region use the country_id we just validated and get the region code if (is_array($region)) { $region['country_id'] = $country_id; $region_id_or_code = $this->_save_region_data($region); } else { $region_id_or_code = $region; } // if we have both a country country id and a region id/code we can construct this object if ($country && $region_id_or_code) { $region_id = WPSC_Countries::get_region_id($country_id, $region_id_or_code); if ($country_id && $region_id) { $wpsc_country = new WPSC_Country($country_id); $wpsc_region = $wpsc_country->get_region($region_id); if ($wpsc_region) { $this->_code = $wpsc_region->_code; $this->_id = $wpsc_region->_id; $this->_country_id = $wpsc_region->_country_id; $this->_name = $wpsc_region->_name; $this->_tax = $wpsc_region->_tax; } } } }
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_get_region() { $country = new WPSC_Country(self::COUNTRY_ID_WITH_REGIONS); $region = $country->get_region(self::REGION_ID); $this->assertInstanceOf('WPSC_Region', $region); $this->assertEquals(self::REGION_ID, $region->id); $this->assertEquals(self::REGION_NAME, $region->name); }
/** * get the output used to show a shipping state and region select drop down * * @since 3.8.14 * * @param wpsc_checkout|null $wpsc_checkout checkout object * @return string */ function wpsc_checkout_shipping_state_and_region($wpsc_checkout = null) { // just in case the checkout form was not presented, like when we are doing the shipping calculator if (empty($wpsc_checkout)) { $wpsc_checkout = new wpsc_checkout(); $doing_checkout_form = false; } else { $doing_checkout_form = true; } // if we aren't showing the shipping state on the cor we have no work to do if (!$wpsc_checkout->get_checkout_item('shippingstate')) { return ''; } // save the current checkout item in case we adjust it in the routine, we'll put it back before return $saved_checkout_item = $wpsc_checkout->checkout_item; // check a new checkout form with all fields $checkout_form = new WPSC_Checkout_Form(null, false); // is the shipping country visible on the form, let's find out $shipping_country_form_element = $checkout_form->get_field_by_unique_name('shippingcountry'); $showing_shipping_country = (bool) $shipping_country_form_element->active; // make sure the shipping state is the current checkout element $wpsc_checkout->checkout_item = $wpsc_checkout->get_checkout_item('shippingstate'); // setup the edit field, aka 'shippingstate' $shipping_country = wpsc_get_customer_meta('shippingcountry'); $shipping_region = wpsc_get_customer_meta('shippingregion'); $shipping_state = wpsc_get_customer_meta('shippingstate'); // if we are showing the billing country on the form then we use the value that can be // changed by the user, otherwise we will use the base country as configured in store admin if ($showing_shipping_country) { $wpsc_country = new WPSC_Country($shipping_country); } else { $wpsc_country = new WPSC_Country(wpsc_get_base_country()); } $region_list = $wpsc_country->get_regions(); $placeholder = $wpsc_country->get('region_label'); if (empty($placeholder)) { $placeholder = $wpsc_checkout->checkout_item->name; } $placeholder = apply_filters('wpsc_checkout_field_placeholder', apply_filters('wpsc_checkout_field_name', $placeholder), $wpsc_checkout->checkout_item); $form_element_id = $wpsc_checkout->form_element_id(); if ($doing_checkout_form) { $id_attribute = ' id="' . $form_element_id . '" '; } else { $id_attribute = ''; } // if there are regions for the current country we are going to // create the billing state edit, but hide it $style = ' '; if (!empty($region_list)) { $style = 'style="display: none;"'; } $output = '<input class="shipping_region text wpsc-visitor-meta" ' . ' data-wpsc-meta-key="' . $wpsc_checkout->checkout_item->unique_name . '" ' . ' title="' . $wpsc_checkout->checkout_item->unique_name . '" ' . ' type="text" ' . $id_attribute . ' placeholder="' . esc_attr($placeholder) . '" ' . ' value="' . esc_attr($shipping_state) . '" ' . ' name="collected_data[' . $wpsc_checkout->checkout_item->id . ']" ' . $style . ' />' . "\n\r"; // setup the drop down field, aka 'shippingregion' // move the checkout item pointer to the billing country, so we can generate form element ids, highly lame $wpsc_checkout->checkout_item = $checkout_form->get_field_by_unique_name('shippingcountry'); // if there aren't any regions for the current country we are going to // create the empty region select, but hide it $style = ' '; if (empty($region_list)) { $style = 'style="display: none;"'; } $title = 'shippingregion'; $region_form_id = $wpsc_checkout->form_element_id() . '_region'; $output .= '<select id="' . $region_form_id . '" ' . ' class="current_region wpsc-visitor-meta wpsc-region-dropdown" ' . ' data-wpsc-meta-key="shippingregion" ' . ' title="' . $title . '" ' . 'name="collected_data[' . $wpsc_checkout->checkout_item->id . '][1]" ' . $style . ">\n\r"; $wpsc_current_region = $wpsc_country->get_region($shipping_region); if (!empty($region_list)) { if (count($region_list) > 1) { $label = $wpsc_country->get('region_label'); $please_select_message = sprintf(__('Please select a %s', 'wp-e-commerce'), $label); $output .= "<option value='0'>" . $please_select_message . "</option>\n\r"; } foreach ($region_list as $wpsc_region) { if ((bool) $wpsc_current_region && $wpsc_current_region->get_id() == $wpsc_region->get_id()) { $selected = "selected='selected'"; } else { $selected = ''; } $output .= "<option value='" . $wpsc_region->get_id() . "' {$selected}>" . esc_html($wpsc_region->get_name()) . "</option>\n\r"; } } $output .= "</select>\n\r"; // restore the checkout item in case we messed with it $wpsc_checkout->checkout_item = $saved_checkout_item; return $output; }
/** * 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(); } }
/** * @description: wpec_taxes_get_region_code_by_id - given an id this funciton will * return the region code. * @param: id - a region id * @return: int or false * */ function wpec_taxes_get_region_code_by_id($region) { $region_code = false; if (!empty($region)) { $country_id = WPSC_Countries::get_country_id_by_region_id($region); if ($country_id) { $wpsc_country = new WPSC_Country($country_id); } if (isset($wpsc_country)) { $wpsc_region = $wpsc_country->get_region($region); if ($wpsc_region) { $region_code = $wpsc_region->get_code(); } } } return $region_code; }
/** * 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)) { $wpsc_region = $wpsc_country->get_region($region); $checkout_item_values = $wpsc_region->get_name(); } } } } 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)) { $wpsc_region = $wpsc_country->get_region($region); $checkout_item_values = $wpsc_region->get_name(); } } } } 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')); } } }