/**
 * 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;
}
Esempio n. 2
0
function wpsc_shipping_country_list($shippingdetails = false)
{
    global $wpsc_shipping_modules;
    $wpsc_checkout = new wpsc_checkout();
    $wpsc_checkout->checkout_item = $shipping_country_checkout_item = $wpsc_checkout->get_checkout_item('shippingcountry');
    $output = '';
    if ($shipping_country_checkout_item && $shipping_country_checkout_item->active) {
        if (!$shippingdetails) {
            $output = "<input type='hidden' name='wpsc_ajax_action' value='update_location' />";
        }
        $acceptable_countries = wpsc_get_acceptable_countries();
        // if there is only one country to choose from we are going to set that as the shipping country,
        // later in the UI generation the same thing will happen to make the single country the current
        // selection
        $countries = WPSC_Countries::get_countries(false);
        if (count($countries) == 1) {
            reset($countries);
            $id_of_only_country_available = key($countries);
            $wpsc_country = new WPSC_Country($id_of_only_country_available);
            wpsc_update_customer_meta('shippingcountry', $wpsc_country->get_isocode());
        }
        $selected_country = wpsc_get_customer_meta('shippingcountry');
        $additional_attributes = 'data-wpsc-meta-key="shippingcountry" ';
        $output .= wpsc_get_country_dropdown(array('id' => 'current_country', 'name' => 'country', 'class' => 'current_country wpsc-visitor-meta', 'acceptable_ids' => $acceptable_countries, 'selected' => $selected_country, 'additional_attributes' => $additional_attributes, 'placeholder' => __('Please select a country', 'wp-e-commerce')));
    }
    $output .= wpsc_checkout_shipping_state_and_region();
    $zipvalue = (string) wpsc_get_customer_meta('shippingpostcode');
    $zip_code_text = __('Your Zipcode', 'wp-e-commerce');
    if ($zipvalue != '' && $zipvalue != $zip_code_text) {
        $color = '#000';
        wpsc_update_customer_meta('shipping_zip', $zipvalue);
    } else {
        $zipvalue = $zip_code_text;
        $color = '#999';
    }
    $uses_zipcode = false;
    $custom_shipping = get_option('custom_shipping_options');
    foreach ((array) $custom_shipping as $shipping) {
        if (isset($wpsc_shipping_modules[$shipping]->needs_zipcode) && $wpsc_shipping_modules[$shipping]->needs_zipcode == true) {
            $uses_zipcode = true;
        }
    }
    if ($uses_zipcode) {
        $output .= " <input data-wpsc-meta-key='shippingpostcode' class='wpsc-visitor-meta' type='text' style='color:" . $color . ";' onclick='if (this.value==\"" . esc_js($zip_code_text) . "\") {this.value=\"\";this.style.color=\"#000\";}' onblur='if (this.value==\"\") {this.style.color=\"#999\"; this.value=\"" . esc_js($zip_code_text) . "\"; }' value='" . esc_attr($zipvalue) . "' size='10' name='zipcode' id='zipcode'>";
    }
    return $output;
}