<label for="agree"><input id="agree" type='checkbox' value='yes' name='agree' /> <?php 
    printf(__("I agree to the <a class='thickbox' target='_blank' href='%s' class='termsandconds'>Terms and Conditions</a>", 'wp-e-commerce'), esc_url(home_url("?termsandconds=true&amp;width=360&amp;height=400")));
    ?>
 <span class="asterix">*</span></label>
               </td>
         </tr>
      <?php 
}
?>
      </table>

<!-- div for make purchase button -->
      <div class='wpsc_make_purchase'>
         <span>
            <?php 
if (!wpsc_has_tnc()) {
    ?>
               <input type='hidden' value='yes' name='agree' />
            <?php 
}
?>
               <input type='hidden' value='submit_checkout' name='wpsc_action' />
               <input type='submit' value='<?php 
_e('Purchase', 'wp-e-commerce');
?>
' class='make_purchase wpsc_buy_button' />
         </span>
      </div>

<div class='clear'></div>
</form>
Пример #2
0
/**
 * submit checkout function, used through ajax and in normal page loading.
 * No parameters, returns nothing
 */
function wpsc_submit_checkout($collected_data = true)
{
    global $wpdb, $wpsc_cart, $user_ID, $nzshpcrt_gateways, $wpsc_shipping_modules, $wpsc_gateways;
    $num_items = 0;
    $use_shipping = 0;
    $disregard_shipping = 0;
    do_action('wpsc_before_submit_checkout');
    $error_messages = wpsc_get_customer_meta('checkout_misc_error_messages');
    if (!is_array($error_messages)) {
        $error_messages = array();
    }
    $wpsc_checkout = new wpsc_checkout();
    $selected_gateways = get_option('custom_gateway_options');
    $submitted_gateway = isset($_POST['custom_gateway']) ? $_POST['custom_gateway'] : '';
    $options = get_option('custom_shipping_options');
    if ($collected_data) {
        $form_validity = $wpsc_checkout->validate_forms();
        extract($form_validity);
        // extracts $is_valid and $error_messages
        if (wpsc_has_tnc() && (!isset($_POST['agree']) || $_POST['agree'] != 'yes')) {
            $error_messages[] = __('Please agree to the terms and conditions, otherwise we cannot process your order.', 'wpsc');
            $is_valid = false;
        }
    } else {
        $is_valid = true;
        $error_messages = array();
    }
    $selectedCountry = $wpdb->get_results($wpdb->prepare("SELECT id, country FROM `" . WPSC_TABLE_CURRENCY_LIST . "` WHERE isocode = '%s' ", wpsc_get_customer_meta('shipping_country')), ARRAY_A);
    foreach ($wpsc_cart->cart_items as $cartitem) {
        if (!empty($cartitem->meta[0]['no_shipping'])) {
            continue;
        }
        $categoriesIDs = $cartitem->category_id_list;
        foreach ((array) $categoriesIDs as $catid) {
            if (is_array($catid)) {
                $countries = wpsc_get_meta($catid[0], 'target_market', 'wpsc_category');
            } else {
                $countries = wpsc_get_meta($catid, 'target_market', 'wpsc_category');
            }
            if (!empty($countries) && !in_array($selectedCountry[0]['id'], (array) $countries)) {
                $errormessage = sprintf(__('%s cannot be shipped to %s. To continue with your transaction please remove this product from the list below.', 'wpsc'), $cartitem->get_title(), $selectedCountry[0]['country']);
                wpsc_update_customer_meta('category_shipping_conflict', $errormessage);
                $is_valid = false;
            }
        }
        //count number of items, and number of items using shipping
        $num_items++;
        if ($cartitem->uses_shipping != 1) {
            $disregard_shipping++;
        } else {
            $use_shipping++;
        }
    }
    if (array_search($submitted_gateway, $selected_gateways) !== false) {
        wpsc_update_customer_meta('selected_gateway', $submitted_gateway);
    } else {
        $is_valid = false;
    }
    if ($collected_data) {
        if (get_option('do_not_use_shipping') == 0 && ($wpsc_cart->selected_shipping_method == null || $wpsc_cart->selected_shipping_option == null) && $num_items != $disregard_shipping) {
            $error_messages[] = __('You must select a shipping method, otherwise we cannot process your order.', 'wpsc');
            $is_valid = false;
        }
        if (get_option('do_not_use_shipping') != 1 && in_array('ups', (array) $options) && !wpsc_get_customer_meta('shipping_zip') && $num_items != $disregard_shipping) {
            wpsc_update_customer_meta('category_shipping_conflict', __('Please enter a Zipcode and click calculate to proceed', 'wpsc'));
            $is_valid = false;
        }
    }
    wpsc_update_customer_meta('checkout_misc_error_messages', $error_messages);
    if ($is_valid == true) {
        wpsc_delete_customer_meta('category_shipping_conflict');
        // check that the submitted gateway is in the list of selected ones
        $sessionid = mt_rand(100, 999) . time();
        wpsc_update_customer_meta('checkout_session_id', $sessionid);
        $subtotal = $wpsc_cart->calculate_subtotal();
        if ($wpsc_cart->has_total_shipping_discount() == false) {
            $base_shipping = $wpsc_cart->calculate_base_shipping();
        } else {
            $base_shipping = 0;
        }
        $delivery_country = $wpsc_cart->delivery_country;
        $delivery_region = $wpsc_cart->delivery_region;
        if (wpsc_uses_shipping()) {
            $shipping_method = $wpsc_cart->selected_shipping_method;
            $shipping_option = $wpsc_cart->selected_shipping_option;
        } else {
            $shipping_method = '';
            $shipping_option = '';
        }
        if (isset($_POST['how_find_us'])) {
            $find_us = $_POST['how_find_us'];
        } else {
            $find_us = '';
        }
        //keep track of tax if taxes are exclusive
        $wpec_taxes_controller = new wpec_taxes_controller();
        if (!$wpec_taxes_controller->wpec_taxes_isincluded()) {
            $tax = $wpsc_cart->calculate_total_tax();
            $tax_percentage = $wpsc_cart->tax_percentage;
        } else {
            $tax = 0.0;
            $tax_percentage = 0.0;
        }
        $total = $wpsc_cart->calculate_total_price();
        $args = array('totalprice' => $total, 'statusno' => '0', 'sessionid' => $sessionid, 'user_ID' => (int) $user_ID, 'date' => time(), 'gateway' => $submitted_gateway, 'billing_country' => $wpsc_cart->selected_country, 'shipping_country' => $delivery_country, 'billing_region' => $wpsc_cart->selected_region, 'shipping_region' => $delivery_region, 'base_shipping' => $base_shipping, 'shipping_method' => $shipping_method, 'shipping_option' => $shipping_option, 'plugin_version' => WPSC_VERSION, 'discount_value' => $wpsc_cart->coupons_amount, 'discount_data' => $wpsc_cart->coupons_name, 'find_us' => $find_us, 'wpec_taxes_total' => $tax, 'wpec_taxes_rate' => $tax_percentage);
        $purchase_log = new WPSC_Purchase_Log($args);
        $purchase_log->save();
        $purchase_log_id = $purchase_log->get('id');
        if ($collected_data) {
            $wpsc_checkout->save_forms_to_db($purchase_log_id);
        }
        $wpsc_cart->save_to_db($purchase_log_id);
        $wpsc_cart->submit_stock_claims($purchase_log_id);
        if (get_option('wpsc_also_bought') == 1) {
            wpsc_populate_also_bought_list();
        }
        if (!isset($our_user_id) && isset($user_ID)) {
            $our_user_id = $user_ID;
        }
        $wpsc_cart->log_id = $purchase_log_id;
        do_action('wpsc_submit_checkout', array("purchase_log_id" => $purchase_log_id, "our_user_id" => $our_user_id));
        if (get_option('permalink_structure') != '') {
            $separator = "?";
        } else {
            $separator = "&";
        }
        // submit to gateway
        $current_gateway_data =& $wpsc_gateways[$submitted_gateway];
        if (isset($current_gateway_data['api_version']) && $current_gateway_data['api_version'] >= 2.0) {
            $merchant_instance = new $current_gateway_data['class_name']($purchase_log_id);
            $merchant_instance->construct_value_array();
            do_action_ref_array('wpsc_pre_submit_gateway', array(&$merchant_instance));
            $merchant_instance->submit();
        } elseif ($current_gateway_data['internalname'] == $submitted_gateway && $current_gateway_data['internalname'] != 'google') {
            $gateway_used = $current_gateway_data['internalname'];
            $purchase_log->set('gateway', $gateway_used);
            $purchase_log->save();
            $current_gateway_data['function']($separator, $sessionid);
        } elseif ($current_gateway_data['internalname'] == 'google' && $current_gateway_data['internalname'] == $submitted_gateway) {
            $gateway_used = $current_gateway_data['internalname'];
            $purchase_log->set('gateway', $gateway_used);
            wpsc_update_customer_meta('google_checkout', 'google');
            wp_redirect(get_option('shopping_cart_url'));
            exit;
        }
    }
}
Пример #3
0
function _wpsc_convert_checkout_form_fields($customer_settings = false)
{
    $form = WPSC_Checkout_Form::get();
    $fields = $form->get_fields();
    $args = array();
    $purchase_log_exists = false;
    if (!$customer_settings) {
        $purchase_log_id = wpsc_get_customer_meta('current_purchase_log_id');
        $purchase_log_exists = (bool) $purchase_log_id;
        if ($purchase_log_exists) {
            $form_data_obj = new WPSC_Checkout_Form_Data($purchase_log_id);
            $form_raw_data = $form_data_obj->get_raw_data();
            $form_data = array();
            foreach ($form_raw_data as $data) {
                $form_data[$data->id] = $data;
            }
        }
    }
    $i = 0;
    $state_country_pairs = array('billing_state' => array(), 'shipping_state' => array());
    $customer_details = wpsc_get_customer_meta('checkout_details');
    if (!is_array($customer_details)) {
        $customer_details = array();
    }
    foreach ($fields as $field) {
        $id = empty($field->unique_name) ? $field->id : $field->unique_name;
        $default_value = array_key_exists($field->id, $customer_details) ? $customer_details[$field->id] : '';
        if ($purchase_log_exists && $field->type != 'heading' && isset($form_data[$field->id])) {
            $default_value = $form_data[$field->id]->value;
        }
        $field_arr = array('type' => $field->type, 'id' => "wpsc-checkout-field-{$id}", 'title' => esc_html($field->name), 'name' => 'wpsc_checkout_details[' . $field->id . ']', 'value' => wpsc_submitted_value($field->id, $default_value, $_POST['wpsc_checkout_details']));
        $validation_rules = array('trim');
        if ($field->mandatory) {
            $validation_rules[] = 'required';
        }
        $optional_state_field = false;
        if (in_array($field->unique_name, array('billingstate', 'shippingstate'))) {
            $field_arr['type'] = 'select_region';
            /* output states for all countries just in case Javascript doesn't work */
            $field_arr['country'] = 'all';
            if ($field->unique_name == 'billingstate') {
                $state_country_pairs['billing_state']['key'] = $i;
            } else {
                $state_country_pairs['shipping_state']['key'] = $i;
            }
            // optional text field in case the country they select do not have states
            // and JS is disabled either by preferences or on error
            $optional_state_field = true;
            // convert state values in text into proper ID
            $validation_rules[] = '_wpsc_convert_state';
        } elseif (in_array($field->unique_name, array('billingcountry', 'shippingcountry')) || $field->type == 'delivery_country') {
            $field_arr['type'] = 'select_country';
            $validation_rules[] = 'country';
            if ($field->unique_name == 'billingcountry') {
                $state_country_pairs['billing_state']['country_field_id'] = $field->id;
            } else {
                $state_country_pairs['shipping_state']['country_field_id'] = $field->id;
            }
        } elseif ($field->type == 'text') {
            $field_arr['type'] = 'textfield';
        } elseif ($field->type == 'select') {
            $field_arr['options'] = array_flip(unserialize($field->options));
        } elseif ($field->type == 'radio') {
            $field_arr['type'] = 'radios';
            $field_arr['options'] = array_flip(unserialize($field->options));
        } elseif ($field->type == 'checkbox') {
            $field_arr['type'] = 'checkboxes';
            $field_arr['options'] = array_flip(unserialize($field->options));
        } elseif (in_array($field->type, array('address', 'city', 'email'))) {
            $field_arr['type'] = 'textfield';
            if ($field->type == 'email') {
                $validation_rules[] = 'email';
            }
        } elseif ($field->type == 'heading' && $field->unique_name == 'delivertoafriend') {
            $field_arr['shipping_heading'] = true;
        }
        $field_arr['rules'] = implode('|', $validation_rules);
        $args[$i] = $field_arr;
        $i++;
        if ($optional_state_field) {
            $args[$i] = $args[$i - 1];
            $args[$i]['type'] = 'textfield';
            $args[$i]['id'] = 'wpsc-checkout-field-' . $id . '-text';
            $i++;
        }
    }
    if (wpsc_has_tnc() && !$customer_settings) {
        $args[] = array('type' => 'checkbox', 'id' => 'wpsc-terms-and-conditions', 'title' => sprintf(__("I agree to the <a class='thickbox' target='_blank' href='%s' class='termsandconds'>Terms and Conditions</a>", "wpsc"), esc_url(site_url("?termsandconds=true&amp;width=360&amp;height=400"))), 'value' => 1, 'name' => 'wpsc_terms_conditions', 'rules' => 'required', 'checked' => wpsc_submitted_value('wpsc_terms_conditions', 0) == 1);
    }
    foreach ($state_country_pairs as $field) {
        $args[$field['key']]['rules'] .= '|state_of[' . $field['country_field_id'] . ']';
        $args[$field['key']]['rules'] = ltrim($args[$field['key']]['rules'], '|');
    }
    return $args;
}
Пример #4
0
/**
 * submit checkout function, used through ajax and in normal page loading.
 * No parameters, returns nothing
 */
function wpsc_submit_checkout($collected_data = true)
{
    global $wpdb, $wpsc_cart, $user_ID, $nzshpcrt_gateways, $wpsc_shipping_modules, $wpsc_gateways;
    if ($collected_data && isset($_POST['collected_data']) && is_array($_POST['collected_data'])) {
        _wpsc_checkout_customer_meta_update($_POST['collected_data']);
    }
    // initialize our checkout status variab;e, we start be assuming
    // checkout is falid, until we find a reason otherwise
    $is_valid = true;
    $num_items = 0;
    $use_shipping = 0;
    $disregard_shipping = 0;
    do_action('wpsc_before_submit_checkout');
    $error_messages = wpsc_get_customer_meta('checkout_misc_error_messages');
    if (!is_array($error_messages)) {
        $error_messages = array();
    }
    $wpsc_checkout = new wpsc_checkout();
    $selected_gateways = get_option('custom_gateway_options');
    $submitted_gateway = isset($_POST['custom_gateway']) ? $_POST['custom_gateway'] : '';
    if ($collected_data) {
        $form_validity = $wpsc_checkout->validate_forms();
        extract($form_validity);
        // extracts $is_valid and $error_messages
        if (wpsc_has_tnc() && (!isset($_POST['agree']) || $_POST['agree'] != 'yes')) {
            $error_messages[] = __('Please agree to the terms and conditions, otherwise we cannot process your order.', 'wpsc');
            $is_valid = false;
        }
    } else {
        $is_valid = true;
        $error_messages = array();
    }
    $wpsc_country = new WPSC_Country(wpsc_get_customer_meta('shippingcountry'));
    $country_id = $wpsc_country->get_id();
    $country_name = $wpsc_country->get_name();
    foreach ($wpsc_cart->cart_items as $cartitem) {
        if (!empty($cartitem->meta[0]['no_shipping'])) {
            continue;
        }
        $categoriesIDs = $cartitem->category_id_list;
        foreach ((array) $categoriesIDs as $catid) {
            if (is_array($catid)) {
                $countries = wpsc_get_meta($catid[0], 'target_market', 'wpsc_category');
            } else {
                $countries = wpsc_get_meta($catid, 'target_market', 'wpsc_category');
            }
            if (!empty($countries) && !in_array($country_id, (array) $countries)) {
                $errormessage = sprintf(__('%s cannot be shipped to %s. To continue with your transaction please remove this product from the list below.', 'wpsc'), $cartitem->get_title(), $country_name);
                wpsc_update_customer_meta('category_shipping_conflict', $errormessage);
                $is_valid = false;
            }
        }
        //count number of items, and number of items using shipping
        $num_items++;
        if ($cartitem->uses_shipping != 1) {
            $disregard_shipping++;
        } else {
            $use_shipping++;
        }
    }
    // check to see if the current gateway is in the list of available gateways
    if (array_search($submitted_gateway, $selected_gateways) !== false) {
        wpsc_update_customer_meta('selected_gateway', $submitted_gateway);
    } else {
        $is_valid = false;
    }
    if ($collected_data) {
        // Test for required shipping information
        if (wpsc_core_shipping_enabled() && $num_items != $disregard_shipping) {
            // for shipping to work we need a method, option and a quote
            if (!$wpsc_cart->shipping_method_selected() || !$wpsc_cart->shipping_quote_selected()) {
                $error_messages[] = __('Please select one of the available shipping options, then we can process your order.', 'wpsc');
                $is_valid = false;
            }
            // if we don't have a valid zip code ( the function also checks if we need it ) we have an error
            if (!wpsc_have_valid_shipping_zipcode()) {
                wpsc_update_customer_meta('category_shipping_conflict', __('Please enter a Zipcode and click calculate to proceed', 'wpsc'));
                $is_valid = false;
            }
        }
    }
    wpsc_update_customer_meta('checkout_misc_error_messages', $error_messages);
    if ($is_valid == true) {
        wpsc_delete_customer_meta('category_shipping_conflict');
        // check that the submitted gateway is in the list of selected ones
        $sessionid = mt_rand(100, 999) . time();
        wpsc_update_customer_meta('checkout_session_id', $sessionid);
        $subtotal = $wpsc_cart->calculate_subtotal();
        if ($wpsc_cart->has_total_shipping_discount() == false) {
            $base_shipping = $wpsc_cart->calculate_base_shipping();
        } else {
            $base_shipping = 0;
        }
        $delivery_country = $wpsc_cart->delivery_country;
        $delivery_region = $wpsc_cart->delivery_region;
        if (wpsc_uses_shipping()) {
            $shipping_method = $wpsc_cart->selected_shipping_method;
            $shipping_option = $wpsc_cart->selected_shipping_option;
        } else {
            $shipping_method = '';
            $shipping_option = '';
        }
        if (isset($_POST['how_find_us'])) {
            $find_us = $_POST['how_find_us'];
        } else {
            $find_us = '';
        }
        //keep track of tax if taxes are exclusive
        $wpec_taxes_controller = new wpec_taxes_controller();
        if (!$wpec_taxes_controller->wpec_taxes_isincluded()) {
            $tax = $wpsc_cart->calculate_total_tax();
            $tax_percentage = $wpsc_cart->tax_percentage;
        } else {
            $tax = 0.0;
            $tax_percentage = 0.0;
        }
        $total = $wpsc_cart->calculate_total_price();
        $args = array('totalprice' => $total, 'statusno' => '0', 'sessionid' => $sessionid, 'user_ID' => (int) $user_ID, 'date' => time(), 'gateway' => $submitted_gateway, 'billing_country' => $wpsc_cart->selected_country, 'shipping_country' => $delivery_country, 'billing_region' => $wpsc_cart->selected_region, 'shipping_region' => $delivery_region, 'base_shipping' => $base_shipping, 'shipping_method' => $shipping_method, 'shipping_option' => $shipping_option, 'plugin_version' => WPSC_VERSION, 'discount_value' => $wpsc_cart->coupons_amount, 'discount_data' => $wpsc_cart->coupons_name, 'find_us' => $find_us, 'wpec_taxes_total' => $tax, 'wpec_taxes_rate' => $tax_percentage);
        $purchase_log = new WPSC_Purchase_Log($args);
        $purchase_log->save();
        $purchase_log_id = $purchase_log->get('id');
        if ($collected_data) {
            $wpsc_checkout->save_forms_to_db($purchase_log_id);
        }
        $wpsc_cart->save_to_db($purchase_log_id);
        $wpsc_cart->submit_stock_claims($purchase_log_id);
        if (!isset($our_user_id) && isset($user_ID)) {
            $our_user_id = $user_ID;
        }
        $wpsc_cart->log_id = $purchase_log_id;
        do_action('wpsc_submit_checkout', array('purchase_log_id' => $purchase_log_id, 'our_user_id' => $our_user_id));
        do_action('wpsc_submit_checkout_gateway', $submitted_gateway, $purchase_log);
    }
}
Пример #5
0
function _wpsc_convert_checkout_form_fields($customer_settings = false)
{
    $form = WPSC_Checkout_Form::get();
    $fields = $form->get_fields();
    if (empty($fields)) {
        return array();
    }
    $args = array();
    $purchase_log_exists = false;
    $fieldsets = array('billing' => array('type' => 'fieldset', 'title' => apply_filters('wpsc_checkout_billing_header_label', __('<h2>Billing &amp; Shipping Details</h2>', 'wp-e-commerce')), 'id' => 'wpsc-checkout-form-billing', 'fields' => array()), 'shipping' => array('type' => 'fieldset', 'title' => apply_filters('wpsc_checkout_shipping_header_label', __('<h2>Shipping Details</h2>', 'wp-e-commerce')), 'id' => 'wpsc-checkout-form-shipping', 'fields' => array()));
    if (!$customer_settings) {
        $purchase_log_id = wpsc_get_customer_meta('current_purchase_log_id');
        $purchase_log_exists = (bool) $purchase_log_id;
        if ($purchase_log_exists) {
            $form_data_obj = new WPSC_Checkout_Form_Data($purchase_log_id);
            $form_raw_data = $form_data_obj->get_raw_data();
            $form_data = array();
            foreach ($form_raw_data as $data) {
                $form_data[$data->id] = $data;
            }
        }
    }
    $i = 0;
    $state_country_pairs = array('billing_state' => array(), 'shipping_state' => array());
    $customer_details = wpsc_get_customer_meta('checkout_details');
    if (!is_array($customer_details)) {
        $customer_details = array();
    }
    foreach ($fields as $field) {
        $id = empty($field->unique_name) ? $field->id : $field->unique_name;
        $is_shipping = false !== strpos($field->unique_name, 'shipping');
        $is_billing = false !== strpos($field->unique_name, 'billing');
        $default_value = array_key_exists($field->id, $customer_details) ? $customer_details[$field->id] : '';
        /* Doing our college-best to check for one of the two original headings */
        if ('heading' == $field->type && ('delivertoafriend' == $field->unique_name || '1' === $field->id)) {
            continue;
        }
        if ($purchase_log_exists && isset($form_data[$field->id])) {
            $default_value = $form_data[$field->id]->value;
        }
        if (isset($_POST['wpsc_checkout_details'])) {
            $_POST['wpsc_checkout_details'] = wp_unslash($_POST['wpsc_checkout_details']);
        }
        $field_arr = array('type' => $field->type, 'id' => "wpsc-checkout-field-{$id}", 'title' => esc_html($field->name), 'name' => 'wpsc_checkout_details[' . $field->id . ']', 'value' => wpsc_submitted_value($field->id, wp_unslash($default_value), $_POST['wpsc_checkout_details']));
        $validation_rules = array('trim');
        if ($field->mandatory) {
            $validation_rules[] = 'required';
        }
        $optional_state_field = false;
        if (in_array($field->unique_name, array('billingstate', 'shippingstate'))) {
            $field_arr['type'] = 'select_region';
            /* output states for all countries just in case Javascript doesn't work */
            $field_arr['country'] = 'all';
            if ($field->unique_name == 'billingstate') {
                $state_country_pairs['billing_state']['key'] = $i;
            } else {
                $state_country_pairs['shipping_state']['key'] = $i;
            }
            // optional text field in case the country they select do not have states
            // and JS is disabled either by preferences or on error
            $optional_state_field = true;
            // convert state values in text into proper ID
            $validation_rules[] = '_wpsc_convert_state';
        } elseif (in_array($field->unique_name, array('billingcountry', 'shippingcountry')) || $field->type == 'delivery_country') {
            $field_arr['type'] = 'select_country';
            $validation_rules[] = 'country';
            if ($field->unique_name == 'billingcountry') {
                $state_country_pairs['billing_state']['country_field_id'] = $field->id;
            } else {
                $state_country_pairs['shipping_state']['country_field_id'] = $field->id;
            }
        } elseif ($field->type == 'text') {
            $field_arr['type'] = 'textfield';
        } elseif ($field->type == 'select') {
            $field_arr['options'] = array_flip(unserialize($field->options));
        } elseif ($field->type == 'radio') {
            $field_arr['type'] = 'radios';
            $field_arr['options'] = array_flip(unserialize($field->options));
        } elseif ($field->type == 'checkbox') {
            $field_arr['type'] = 'checkboxes';
            $field_arr['options'] = array_flip(unserialize($field->options));
        } elseif (in_array($field->type, array('address', 'city', 'email'))) {
            $field_arr['type'] = 'textfield';
            if ($field->type == 'email') {
                $validation_rules[] = 'email';
            }
        } elseif ($field->type == 'heading' && $field->unique_name == 'delivertoafriend') {
            $field_arr['shipping_heading'] = true;
        }
        $field_arr['rules'] = implode('|', $validation_rules);
        if ($is_shipping) {
            $fieldsets['shipping']['fields'][$i] = $field_arr;
        } else {
            if ($is_billing) {
                $fieldsets['billing']['fields'][$i] = $field_arr;
            } else {
                $args[$i] = $field_arr;
            }
        }
        $i++;
        if ($optional_state_field && $is_billing) {
            $fieldsets['billing']['fields'][$i] = $fieldsets['billing']['fields'][$i - 1];
            $fieldsets['billing']['fields'][$i]['type'] = 'textfield';
            $fieldsets['billing']['fields'][$i]['id'] = 'wpsc-checkout-field-' . $id . '-text';
            $i++;
        } else {
            if ($optional_state_field && $is_shipping) {
                $fieldsets['shipping']['fields'][$i] = $fieldsets['shipping']['fields'][$i - 1];
                $fieldsets['shipping']['fields'][$i]['type'] = 'textfield';
                $fieldsets['shipping']['fields'][$i]['id'] = 'wpsc-checkout-field-' . $id . '-text';
            }
        }
    }
    if (wpsc_has_tnc() && !$customer_settings) {
        $args[] = array('type' => 'checkbox', 'id' => 'wpsc-terms-and-conditions', 'title' => sprintf(__("I agree to the <a class='thickbox' target='_blank' href='%s' class='termsandconds'>Terms and Conditions</a>", 'wp-e-commerce'), esc_url(add_query_arg(array('termsandconds' => 'true', 'width' => 360, 'height' => 400)))), 'value' => 1, 'name' => 'wpsc_terms_conditions', 'rules' => 'required', 'checked' => wpsc_submitted_value('wpsc_terms_conditions', 0) == 1);
    }
    foreach ($state_country_pairs as $state => $field) {
        $is_shipping = 'shipping_state' == $state;
        if (isset($field['key']) && $is_shipping) {
            $fieldsets['shipping']['fields'][$field['key']]['rules'] .= '|state_of[' . $field['country_field_id'] . ']';
            $fieldsets['shipping']['fields'][$field['key']]['rules'] = ltrim($fieldsets['shipping']['fields'][$field['key']]['rules'], '|');
        } else {
            if (isset($field['key'])) {
                $fieldsets['billing']['fields'][$field['key']]['rules'] .= '|state_of[' . $field['country_field_id'] . ']';
                $fieldsets['billing']['fields'][$field['key']]['rules'] = ltrim($fieldsets['billing']['fields'][$field['key']]['rules'], '|');
            }
        }
    }
    /* Add 'shipping same as billing' box to end of billing, rather than shipping header. */
    if (!empty($fieldsets['billing']['fields']) && !empty($fieldsets['shipping']['fields'])) {
        $checked = wpsc_get_customer_meta('wpsc_copy_billing_details');
        $fieldsets['billing']['fields'][$i++] = array('type' => 'checkbox', 'id' => 'wpsc-terms-and-conditions', 'title' => apply_filters('wpsc_shipping_same_as_billing', __('Shipping address is same as billing', 'wp-e-commerce')), 'value' => 1, 'name' => 'wpsc_copy_billing_details', 'checked' => empty($checked) || '1' == $checked);
    }
    if (empty($fieldsets['billing']['fields'])) {
        unset($fieldsets['billing']);
    }
    if (empty($fieldsets['shipping']['fields'])) {
        unset($fieldsets['shipping']);
    }
    return $fieldsets + $args;
}