/** * See if a given postcode matches valid postcodes. * @param string postcode * @param string country code * @return boolean */ public function is_valid_postcode($postcode, $country) { $codes = $this->get_valid_postcodes(); $postcode = $this->clean($postcode); $formatted_postcode = wc_format_postcode($postcode, $country); if (in_array($postcode, $codes) || in_array($formatted_postcode, $codes)) { return true; } // Pattern matching foreach ($codes as $c) { $pattern = '/^' . str_replace('_', '[0-9a-zA-Z]', preg_quote($c)) . '$/i'; if (preg_match($pattern, $postcode)) { return true; } } // Wildcard search $wildcard_postcode = $formatted_postcode . '*'; $postcode_length = strlen($formatted_postcode); for ($i = 0; $i < $postcode_length; $i++) { if (in_array($wildcard_postcode, $codes)) { return true; } $wildcard_postcode = substr($wildcard_postcode, 0, -2) . '*'; } return false; }
/** * Calculate shipping for the cart */ public static function calculate_shipping() { try { WC()->shipping->reset_shipping(); $country = wc_clean($_POST['calc_shipping_country']); $state = wc_clean(isset($_POST['calc_shipping_state']) ? $_POST['calc_shipping_state'] : ''); $postcode = apply_filters('woocommerce_shipping_calculator_enable_postcode', true) ? wc_clean($_POST['calc_shipping_postcode']) : ''; $city = apply_filters('woocommerce_shipping_calculator_enable_city', false) ? wc_clean($_POST['calc_shipping_city']) : ''; if ($postcode && !WC_Validation::is_postcode($postcode, $country)) { throw new Exception(__('Please enter a valid postcode/ZIP.', 'woocommerce')); } elseif ($postcode) { $postcode = wc_format_postcode($postcode, $country); } if ($country) { WC()->customer->set_location($country, $state, $postcode, $city); WC()->customer->set_shipping_location($country, $state, $postcode, $city); } else { WC()->customer->set_to_base(); WC()->customer->set_shipping_to_base(); } WC()->customer->calculated_shipping(true); wc_add_notice(__('Shipping costs updated.', 'woocommerce'), 'notice'); do_action('woocommerce_calculated_shipping'); } catch (Exception $e) { if (!empty($e)) { wc_add_notice($e->getMessage(), 'error'); } } }
/** * Output the cart shortcode. * * @param array $atts */ public static function output($atts) { // Check cart class is loaded or abort if (is_null(WC()->cart)) { return; } // Constants if (!defined('WOOCOMMERCE_CART')) { define('WOOCOMMERCE_CART', true); } // Update Shipping if (!empty($_POST['calc_shipping']) && wp_verify_nonce($_POST['_wpnonce'], 'woocommerce-cart')) { try { WC()->shipping->reset_shipping(); $country = wc_clean($_POST['calc_shipping_country']); $state = isset($_POST['calc_shipping_state']) ? wc_clean($_POST['calc_shipping_state']) : ''; $postcode = apply_filters('woocommerce_shipping_calculator_enable_postcode', true) ? wc_clean($_POST['calc_shipping_postcode']) : ''; $city = apply_filters('woocommerce_shipping_calculator_enable_city', false) ? wc_clean($_POST['calc_shipping_city']) : ''; if ($postcode && !WC_Validation::is_postcode($postcode, $country)) { throw new Exception(__('Please enter a valid postcode/ZIP.', 'woocommerce')); } elseif ($postcode) { $postcode = wc_format_postcode($postcode, $country); } if ($country) { WC()->customer->set_location($country, $state, $postcode, $city); WC()->customer->set_shipping_location($country, $state, $postcode, $city); } else { WC()->customer->set_to_base(); WC()->customer->set_shipping_to_base(); } WC()->customer->calculated_shipping(true); wc_add_notice(__('Shipping costs updated.', 'woocommerce'), 'notice'); do_action('woocommerce_calculated_shipping'); } catch (Exception $e) { if (!empty($e)) { wc_add_notice($e->getMessage(), 'error'); } } } // Check cart items are valid do_action('woocommerce_check_cart_items'); // Calc totals WC()->cart->calculate_totals(); if (sizeof(WC()->cart->get_cart()) == 0) { wc_get_template('cart/cart-empty.php'); } else { wc_get_template('cart/cart.php'); } }
/** * Format the postcode according to the country and length of the postcode * * @param string postcode * @param string country * @return string formatted postcode */ public static function format_postcode($postcode, $country) { wc_format_postcode($postcode, $country); }
/** * Gets the postcode from the current session. * * @return string */ public function get_shipping_postcode() { return empty($this->shipping_postcode) ? '' : wc_format_postcode($this->shipping_postcode, $this->get_shipping_country()); }
/** * Process the checkout after the confirm order button is pressed * * @access public * @return void */ public function process_checkout() { global $wpdb, $current_user; wp_verify_nonce($_POST['_wpnonce'], 'woocommerce-process_checkout'); if (!defined('WOOCOMMERCE_CHECKOUT')) { define('WOOCOMMERCE_CHECKOUT', true); } // Prevent timeout @set_time_limit(0); do_action('woocommerce_before_checkout_process'); if (sizeof(WC()->cart->get_cart()) == 0) { wc_add_notice(sprintf(__('Sorry, your session has expired. <a href="%s" class="wc-backward">Return to homepage</a>', 'woocommerce'), home_url()), 'error'); } do_action('woocommerce_checkout_process'); // Checkout fields (not defined in checkout_fields) $this->posted['terms'] = isset($_POST['terms']) ? 1 : 0; $this->posted['createaccount'] = isset($_POST['createaccount']) ? 1 : 0; $this->posted['payment_method'] = isset($_POST['payment_method']) ? stripslashes($_POST['payment_method']) : ''; $this->posted['shipping_method'] = isset($_POST['shipping_method']) ? $_POST['shipping_method'] : ''; $this->posted['ship_to_different_address'] = isset($_POST['ship_to_different_address']) ? true : false; if (isset($_POST['shiptobilling'])) { _deprecated_argument('WC_Checkout::process_checkout()', '2.1', 'The "shiptobilling" field is deprecated. THe template files are out of date'); $this->posted['ship_to_different_address'] = $_POST['shiptobilling'] ? false : true; } // Ship to billing only option if (WC()->cart->ship_to_billing_address_only()) { $this->posted['ship_to_different_address'] = false; } // Update customer shipping and payment method to posted method $chosen_shipping_methods = WC()->session->get('chosen_shipping_methods'); if (isset($this->posted['shipping_method']) && is_array($this->posted['shipping_method'])) { foreach ($this->posted['shipping_method'] as $i => $value) { $chosen_shipping_methods[$i] = wc_clean($value); } } WC()->session->set('chosen_shipping_methods', $chosen_shipping_methods); WC()->session->set('chosen_payment_method', $this->posted['payment_method']); // Note if we skip shipping $skipped_shipping = false; // Get posted checkout_fields and do validation foreach ($this->checkout_fields as $fieldset_key => $fieldset) { // Skip shipping if not needed if ($fieldset_key == 'shipping' && ($this->posted['ship_to_different_address'] == false || !WC()->cart->needs_shipping())) { $skipped_shipping = true; continue; } // Ship account if not needed if ($fieldset_key == 'account' && (is_user_logged_in() || $this->must_create_account == false && empty($this->posted['createaccount']))) { continue; } foreach ($fieldset as $key => $field) { if (!isset($field['type'])) { $field['type'] = 'text'; } // Get Value switch ($field['type']) { case "checkbox": $this->posted[$key] = isset($_POST[$key]) ? 1 : 0; break; case "multiselect": $this->posted[$key] = isset($_POST[$key]) ? implode(', ', array_map('wc_clean', $_POST[$key])) : ''; break; case "textarea": $this->posted[$key] = isset($_POST[$key]) ? wp_strip_all_tags(wp_check_invalid_utf8(stripslashes($_POST[$key]))) : ''; break; default: $this->posted[$key] = isset($_POST[$key]) ? wc_clean($_POST[$key]) : ''; break; } // Hooks to allow modification of value $this->posted[$key] = apply_filters('woocommerce_process_checkout_' . sanitize_title($field['type']) . '_field', $this->posted[$key]); $this->posted[$key] = apply_filters('woocommerce_process_checkout_field_' . $key, $this->posted[$key]); // Validation: Required fields if (isset($field['required']) && $field['required'] && empty($this->posted[$key])) { wc_add_notice('<strong>' . $field['label'] . '</strong> ' . __('is a required field.', 'woocommerce'), 'error'); } if (!empty($this->posted[$key])) { // Validation rules if (!empty($field['validate']) && is_array($field['validate'])) { foreach ($field['validate'] as $rule) { switch ($rule) { case 'postcode': $this->posted[$key] = strtoupper(str_replace(' ', '', $this->posted[$key])); if (!WC_Validation::is_postcode($this->posted[$key], $_POST[$fieldset_key . '_country'])) { wc_add_notice(__('Please enter a valid postcode/ZIP.', 'woocommerce'), 'error'); } else { $this->posted[$key] = wc_format_postcode($this->posted[$key], $_POST[$fieldset_key . '_country']); } break; case 'phone': $this->posted[$key] = wc_format_phone_number($this->posted[$key]); if (!WC_Validation::is_phone($this->posted[$key])) { wc_add_notice('<strong>' . $field['label'] . '</strong> ' . __('is not a valid phone number.', 'woocommerce'), 'error'); } break; case 'email': $this->posted[$key] = strtolower($this->posted[$key]); if (!is_email($this->posted[$key])) { wc_add_notice('<strong>' . $field['label'] . '</strong> ' . __('is not a valid email address.', 'woocommerce'), 'error'); } break; case 'state': // Get valid states $valid_states = WC()->countries->get_states($_POST[$fieldset_key . '_country']); if ($valid_states) { $valid_state_values = array_flip(array_map('strtolower', $valid_states)); } // Convert value to key if set if (isset($valid_state_values[strtolower($this->posted[$key])])) { $this->posted[$key] = $valid_state_values[strtolower($this->posted[$key])]; } // Only validate if the country has specific state options if ($valid_states && sizeof($valid_states) > 0) { if (!in_array($this->posted[$key], array_keys($valid_states))) { wc_add_notice('<strong>' . $field['label'] . '</strong> ' . __('is not valid. Please enter one of the following:', 'woocommerce') . ' ' . implode(', ', $valid_states), 'error'); } } break; } } } } } } // Update customer location to posted location so we can correctly check available shipping methods if (isset($this->posted['billing_country'])) { WC()->customer->set_country($this->posted['billing_country']); } if (isset($this->posted['billing_state'])) { WC()->customer->set_state($this->posted['billing_state']); } if (isset($this->posted['billing_postcode'])) { WC()->customer->set_postcode($this->posted['billing_postcode']); } // Shipping Information if (!$skipped_shipping) { // Update customer location to posted location so we can correctly check available shipping methods if (isset($this->posted['shipping_country'])) { WC()->customer->set_shipping_country($this->posted['shipping_country']); } if (isset($this->posted['shipping_state'])) { WC()->customer->set_shipping_state($this->posted['shipping_state']); } if (isset($this->posted['shipping_postcode'])) { WC()->customer->set_shipping_postcode($this->posted['shipping_postcode']); } } else { // Update customer location to posted location so we can correctly check available shipping methods if (isset($this->posted['billing_country'])) { WC()->customer->set_shipping_country($this->posted['billing_country']); } if (isset($this->posted['billing_state'])) { WC()->customer->set_shipping_state($this->posted['billing_state']); } if (isset($this->posted['billing_postcode'])) { WC()->customer->set_shipping_postcode($this->posted['billing_postcode']); } } // Update cart totals now we have customer address WC()->cart->calculate_totals(); // Terms if (!isset($_POST['woocommerce_checkout_update_totals']) && empty($this->posted['terms']) && wc_get_page_id('terms') > 0) { wc_add_notice(__('You must accept our Terms & Conditions.', 'woocommerce'), 'error'); } if (WC()->cart->needs_shipping()) { if (!in_array(WC()->customer->get_shipping_country(), array_keys(WC()->countries->get_shipping_countries()))) { wc_add_notice(sprintf(__('Unfortunately <strong>we do not ship to %s</strong>. Please enter an alternative shipping address.', 'woocommerce'), WC()->countries->shipping_to_prefix() . ' ' . WC()->customer->get_shipping_country()), 'error'); } // Validate Shipping Methods $packages = WC()->shipping->get_packages(); $this->shipping_methods = WC()->session->get('chosen_shipping_methods'); foreach ($packages as $i => $package) { if (!isset($package['rates'][$this->shipping_methods[$i]])) { wc_add_notice(__('Invalid shipping method.', 'woocommerce'), 'error'); $this->shipping_methods[$i] = ''; } } } if (WC()->cart->needs_payment()) { // Payment Method $available_gateways = WC()->payment_gateways->get_available_payment_gateways(); if (!isset($available_gateways[$this->posted['payment_method']])) { $this->payment_method = ''; wc_add_notice(__('Invalid payment method.', 'woocommerce'), 'error'); } else { $this->payment_method = $available_gateways[$this->posted['payment_method']]; $this->payment_method->validate_fields(); } } // Action after validation do_action('woocommerce_after_checkout_validation', $this->posted); if (!isset($_POST['woocommerce_checkout_update_totals']) && wc_notice_count('error') == 0) { try { // Customer accounts $this->customer_id = apply_filters('woocommerce_checkout_customer_id', get_current_user_id()); if (!is_user_logged_in() && ($this->must_create_account || !empty($this->posted['createaccount']))) { $username = !empty($this->posted['account_username']) ? $this->posted['account_username'] : ''; $password = !empty($this->posted['account_password']) ? $this->posted['account_password'] : ''; $new_customer = wc_create_new_customer($this->posted['billing_email'], $username, $password); if (is_wp_error($new_customer)) { throw new Exception($new_customer->get_error_message()); } $this->customer_id = $new_customer; wc_set_customer_auth_cookie($this->customer_id); // As we are now logged in, checkout will need to refresh to show logged in data WC()->session->set('reload_checkout', true); // Add customer info from other billing fields if ($this->posted['billing_first_name'] && apply_filters('woocommerce_checkout_update_customer_data', true, $this)) { $userdata = array('ID' => $this->customer_id, 'first_name' => $this->posted['billing_first_name'] ? $this->posted['billing_first_name'] : '', 'last_name' => $this->posted['billing_last_name'] ? $this->posted['billing_last_name'] : '', 'display_name' => $this->posted['billing_first_name'] ? $this->posted['billing_first_name'] : ''); wp_update_user(apply_filters('woocommerce_checkout_customer_userdata', $userdata, $this)); } } // Do a final stock check at this point $this->check_cart_items(); // Abort if errors are present if (wc_notice_count('error') > 0) { throw new Exception(); } $order_id = $this->create_order(); do_action('woocommerce_checkout_order_processed', $order_id, $this->posted); // Process payment if (WC()->cart->needs_payment()) { // Store Order ID in session so it can be re-used after payment failure WC()->session->order_awaiting_payment = $order_id; // Process Payment $result = $available_gateways[$this->posted['payment_method']]->process_payment($order_id); // Redirect to success/confirmation/payment page if ($result['result'] == 'success') { $result = apply_filters('woocommerce_payment_successful_result', $result, $order_id); if (is_ajax()) { echo '<!--WC_START-->' . json_encode($result) . '<!--WC_END-->'; exit; } else { wp_redirect($result['redirect']); exit; } } } else { if (empty($order)) { $order = new WC_Order($order_id); } // No payment was required for order $order->payment_complete(); // Empty the Cart WC()->cart->empty_cart(); // Get redirect $return_url = $order->get_checkout_order_received_url(); // Redirect to success/confirmation/payment page if (is_ajax()) { echo '<!--WC_START-->' . json_encode(array('result' => 'success', 'redirect' => apply_filters('woocommerce_checkout_no_payment_needed_redirect', $return_url, $order))) . '<!--WC_END-->'; exit; } else { wp_safe_redirect(apply_filters('woocommerce_checkout_no_payment_needed_redirect', $return_url, $order)); exit; } } } catch (Exception $e) { if (!empty($e)) { wc_add_notice($e->getMessage(), 'error'); } } } // endif // If we reached this point then there were errors if (is_ajax()) { ob_start(); wc_print_notices(); $messages = ob_get_clean(); echo '<!--WC_START-->' . json_encode(array('result' => 'failure', 'messages' => $messages, 'refresh' => isset(WC()->session->refresh_totals) ? 'true' : 'false', 'reload' => isset(WC()->session->reload_checkout) ? 'true' : 'false')) . '<!--WC_END-->'; unset(WC()->session->refresh_totals, WC()->session->reload_checkout); exit; } }
/** * Re-calculate a shipping and tax estimate when on the cart page. * * The WC_Shortcode_Cart actually calculates shipping when the "Calculate Shipping" form is submitted on the * cart page. Because of that, our own @see self::calculate_totals() method calculates incorrect values on * the cart page because it triggers the method multiple times for multiple different pricing structures. * This uses the same logic found in WC_Shortcode_Cart::output() to determine the correct estimate. * * @since 1.4.10 */ private static function maybe_recalculate_shipping() { if (!empty($_POST['calc_shipping']) && wp_verify_nonce($_POST['_wpnonce'], 'woocommerce-cart') && function_exists('WC')) { try { WC()->shipping->reset_shipping(); $country = wc_clean($_POST['calc_shipping_country']); $state = isset($_POST['calc_shipping_state']) ? wc_clean($_POST['calc_shipping_state']) : ''; $postcode = apply_filters('woocommerce_shipping_calculator_enable_postcode', true) ? wc_clean($_POST['calc_shipping_postcode']) : ''; $city = apply_filters('woocommerce_shipping_calculator_enable_city', false) ? wc_clean($_POST['calc_shipping_city']) : ''; if ($postcode && !WC_Validation::is_postcode($postcode, $country)) { throw new Exception(__('Please enter a valid postcode/ZIP.', 'woocommerce')); } elseif ($postcode) { $postcode = wc_format_postcode($postcode, $country); } if ($country) { WC()->customer->set_location($country, $state, $postcode, $city); WC()->customer->set_shipping_location($country, $state, $postcode, $city); } else { WC()->customer->set_to_base(); WC()->customer->set_shipping_to_base(); } WC()->customer->calculated_shipping(true); do_action('woocommerce_calculated_shipping'); } catch (Exception $e) { // Exception will have also been caught and displayed in WC_Shortcode_Cart::output() } } }
/** * is_available function. * * @access public * @param array $package * @return bool */ function is_available($package) { $is_available = true; if ($this->enabled == "no") { $is_available = false; } else { // If post codes are listed, let's use them. $codes = array(); if ($this->codes != '') { foreach (explode(',', $this->codes) as $code) { $codes[] = strtoupper(trim($code)); } } if (!empty($codes)) { $found_match = false; $postcode = $this->clean($package['destination']['postcode']); $formatted_postcode = wc_format_postcode($postcode, $package['destination']['country']); if (in_array($postcode, $codes) || in_array($formatted_postcode, $codes)) { $found_match = true; } // Wildcard search if (!$found_match) { $customer_postcode = $formatted_postcode; $customer_postcode_length = strlen($customer_postcode); for ($i = 0; $i <= $customer_postcode_length; $i++) { if (in_array($customer_postcode, $codes)) { $found_match = true; break; } $customer_postcode = substr($customer_postcode, 0, -2) . '*'; } } if (!$found_match) { $is_available = false; } else { if ($this->availability === 'specific') { $ship_to_countries = $this->countries; } else { $ship_to_countries = array_keys(WC()->countries->get_shipping_countries()); } if (is_array($ship_to_countries) && !in_array($package['destination']['country'], $ship_to_countries)) { $is_available = false; } } } else { if ($this->availability === 'specific') { $ship_to_countries = $this->countries; } else { $ship_to_countries = array_keys(WC()->countries->get_shipping_countries()); } if (is_array($ship_to_countries) && !in_array($package['destination']['country'], $ship_to_countries)) { $is_available = false; } } } return apply_filters('woocommerce_shipping_' . $this->id . '_is_available', $is_available, $package); }
/** * Return a list of potential postcodes for wildcard searching. * @since 2.6.0 * @param string $postcode * @param string $country to format postcode for matching. * @return string[] */ function wc_get_wildcard_postcodes($postcode, $country = '') { $postcodes = array($postcode); $postcode = wc_format_postcode($postcode, $country); $postcodes[] = $postcode; $postcode_length = strlen($postcode); for ($i = 0; $i < $postcode_length; $i++) { $postcodes[] = substr($postcode, 0, ($i + 1) * -1) . '*'; } return $postcodes; }
/** * Gets the postcode from the current session. * * @access public * @return string */ public function get_shipping_postcode() { if (isset($this->shipping_postcode)) { return wc_format_postcode($this->shipping_postcode, $this->get_shipping_country()); } }
public function get_woocommerce_zipcode($code) { if (!$this->is_ajax()) { return $code; } $code = wc_format_postcode($_REQUEST['postcode'], WC()->customer->get_shipping_country()); return $code; }
/** * Test wc_format_postcode(). * * @since 2.2 */ public function test_wc_format_postcode() { // generic postcode $this->assertEquals('02111', wc_format_postcode(' 02111 ', 'US')); // UK postcode $this->assertEquals('PCRN 1ZZ', wc_format_postcode('pcrn1zz', 'GB')); }
function save_address_book() { global $woocommerce; $this->load_cart_files(); $checkout = new WC_Checkout(); $user = wp_get_current_user(); $address = $_POST['address']; $shipFields = $woocommerce->countries->get_address_fields($address['shipping_country'], 'shipping_'); $errors = array(); foreach ($shipFields as $key => $field) { if (isset($field['required']) && $field['required'] && empty($address[$key])) { $errors[] = $key; } if (!empty($address[$key])) { // Validation rules if (!empty($field['validate']) && is_array($field['validate'])) { foreach ($field['validate'] as $rule) { switch ($rule) { case 'postcode': $address[$key] = strtoupper(str_replace(' ', '', $address[$key])); if (!WC_Validation::is_postcode($address[$key], $address['shipping_country'])) { $errors[] = $key; wc_add_notice(__('Please enter a valid postcode/ZIP.', 'woocommerce'), 'error'); } else { $address[$key] = wc_format_postcode($address[$key], $address['shipping_country']); } break; case 'phone': $address[$key] = wc_format_phone_number($address[$key]); if (!WC_Validation::is_phone($address[$key])) { $errors[] = $key; if (function_exists('wc_add_notice')) { wc_add_notice('<strong>' . $field['label'] . '</strong> ' . __('is not a valid phone number.', 'woocommerce'), 'error'); } else { $woocommerce->add_error('<strong>' . $field['label'] . '</strong> ' . __('is not a valid phone number.', 'woocommerce')); } } break; case 'email': $address[$key] = strtolower($address[$key]); if (!is_email($address[$key])) { $errors[] = $key; if (function_exists('wc_add_notice')) { wc_add_notice('<strong>' . $field['label'] . '</strong> ' . __('is not a valid email address.', 'woocommerce'), 'error'); } else { $woocommerce->add_error('<strong>' . $field['label'] . '</strong> ' . __('is not a valid email address.', 'woocommerce')); } } break; case 'state': // Get valid states $valid_states = WC()->countries->get_states($address['shipping_country']); if ($valid_states) { $valid_state_values = array_flip(array_map('strtolower', $valid_states)); } // Convert value to key if set if (isset($valid_state_values[strtolower($address[$key])])) { $address[$key] = $valid_state_values[strtolower($address[$key])]; } // Only validate if the country has specific state options if (is_array($valid_states) && sizeof($valid_states) > 0) { if (!in_array($address[$key], array_keys($valid_states))) { $errors[] = $key; if (function_exists('wc_add_notice')) { wc_add_notice('<strong>' . $field['label'] . '</strong> ' . __('is not valid. Please enter one of the following:', 'woocommerce') . ' ' . implode(', ', $valid_states), 'error'); } else { $woocommerce->add_error('<strong>' . $field['label'] . '</strong> ' . __('is not valid. Please enter one of the following:', 'woocommerce') . ' ' . implode(', ', $valid_states)); } } } break; } } } } } if (count($errors) > 0) { die(json_encode(array('ack' => 'ERR', 'errors' => $errors, 'message' => __('Please enter the complete address', 'wc_shipping_multiple_address')))); } $id = $_POST['id']; $addresses = $this->get_user_addresses($user); $redirect_url = isset($_POST['next']) ? $_POST['next'] : get_permalink(woocommerce_get_page_id('multiple_addresses')); if ($id >= 0) { $next = add_query_arg('updated', '1', $redirect_url); } else { $next = add_query_arg('new', '1', $redirect_url); } // address is unique, save! if ($id == -1) { $vals = ''; foreach ($address as $key => $value) { $vals .= $value; } $md5 = md5($vals); foreach ($addresses as $addr) { $vals = ''; if (!is_array($addr)) { continue; } foreach ($addr as $key => $value) { $vals .= $value; } $addrMd5 = md5($vals); if ($md5 == $addrMd5) { // duplicate address! die(json_encode(array('ack' => 'ERR', 'message' => __('Address is already in your address book', 'wc_shipping_multiple_address')))); } } $addresses[] = $address; } else { $addresses[$id] = $address; } // update the default address and remove it from the $addresses array if ($user->ID > 0) { if ($id == 0) { $default_address = $addresses[0]; unset($addresses[0]); if ($default_address['shipping_address_1'] && $default_address['shipping_postcode']) { update_user_meta($user->ID, 'shipping_first_name', $default_address['shipping_first_name']); update_user_meta($user->ID, 'shipping_last_name', $default_address['shipping_last_name']); update_user_meta($user->ID, 'shipping_company', $default_address['shipping_company']); update_user_meta($user->ID, 'shipping_address_1', $default_address['shipping_address_1']); update_user_meta($user->ID, 'shipping_address_2', $default_address['shipping_address_2']); update_user_meta($user->ID, 'shipping_city', $default_address['shipping_city']); update_user_meta($user->ID, 'shipping_state', $default_address['shipping_state']); update_user_meta($user->ID, 'shipping_postcode', $default_address['shipping_postcode']); update_user_meta($user->ID, 'shipping_country', $default_address['shipping_country']); } unset($addresses[0]); } } $this->save_user_addresses($user->ID, $addresses); foreach ($address as $key => $value) { $new_key = str_replace('shipping_', '', $key); $address[$new_key] = $value; } $formatted_address = wcms_get_formatted_address($address); $json_address = json_encode($address); if (!$formatted_address) { return; } if (isset($_POST['return']) && $_POST['return'] == 'list') { $html = '<option value="' . $id . '">' . $formatted_address . '</option>'; } else { $html = ' <div class="account-address"> <address>' . $formatted_address . '</address> <div style="display: none;">'; ob_start(); foreach ($shipFields as $key => $field) { $val = isset($address[$key]) ? $address[$key] : ''; $key .= '_' . $id; woocommerce_form_field($key, $field, $val); } do_action('woocommerce_after_checkout_shipping_form', $checkout); $html .= ob_get_clean(); $html .= ' <input type="hidden" name="addresses[]" value="' . $id . '" /> </div> <ul class="items-column" id="items_column_' . $id . '"> <li class="placeholder">Drag items here</li> </ul> </div> '; } $return = json_encode(array('ack' => 'OK', 'id' => $id, 'html' => $html, 'return' => $_POST['return'], 'next' => $next)); die($return); }
/** * Re-calculate a shipping and tax estimate when on the cart page. * * The WC_Shortcode_Cart actually calculates shipping when the "Calculate Shipping" form is submitted on the * cart page. Because of that, our own @see self::calculate_totals() method calculates incorrect values on * the cart page because it triggers the method multiple times for multiple different pricing structures. * This uses the same logic found in WC_Shortcode_Cart::output() to determine the correct estimate. * * @since 1.4.10 */ private static function maybe_recalculate_shipping() { if (!empty($_POST['calc_shipping']) && wp_verify_nonce($_POST['_wpnonce'], 'woocommerce-cart') && function_exists('WC')) { try { WC()->shipping->reset_shipping(); $country = wc_clean($_POST['calc_shipping_country']); $state = isset($_POST['calc_shipping_state']) ? wc_clean($_POST['calc_shipping_state']) : ''; $postcode = apply_filters('woocommerce_shipping_calculator_enable_postcode', true) ? wc_clean($_POST['calc_shipping_postcode']) : ''; $city = apply_filters('woocommerce_shipping_calculator_enable_city', false) ? wc_clean($_POST['calc_shipping_city']) : ''; if ($postcode && !WC_Validation::is_postcode($postcode, $country)) { throw new Exception(__('Please enter a valid postcode/ZIP.', 'woocommerce-subscriptions')); } elseif ($postcode) { $postcode = wc_format_postcode($postcode, $country); } if ($country) { WC()->customer->set_location($country, $state, $postcode, $city); WC()->customer->set_shipping_location($country, $state, $postcode, $city); } else { WC()->customer->set_to_base(); WC()->customer->set_shipping_to_base(); } WC()->customer->calculated_shipping(true); do_action('woocommerce_calculated_shipping'); } catch (Exception $e) { if (!empty($e)) { wc_add_notice($e->getMessage(), 'error'); } } } // If we had one time shipping in the carts, we may have wiped the WC chosen shippings. Restore them. self::maybe_restore_chosen_shipping_method(); // Now make sure the correct shipping method is set $chosen_shipping_methods = WC()->session->get('chosen_shipping_methods', array()); if (isset($_POST['shipping_method']) && is_array($_POST['shipping_method'])) { foreach ($_POST['shipping_method'] as $i => $value) { $chosen_shipping_methods[$i] = wc_clean($value); } } WC()->session->set('chosen_shipping_methods', $chosen_shipping_methods); }
/** * Edit subscription shipping address * * @access public * @param int $subscription_id * @return void */ public static function subscription_address($subscription_id) { if ($subscription = self::get_subscription($subscription_id)) { if (!$subscription->needs_shipping() || !apply_filters('subscriptio_allow_shipping_address_edit', true)) { self::redirect_to_subscription($subscription); return; } // Form submitted? if (isset($_POST['action']) && $_POST['action'] == 'subscriptio_edit_address') { // Validate address WooCommerce-style $address = WC()->countries->get_address_fields(esc_attr($_POST['shipping_country' ]), 'shipping_'); foreach ($address as $key => $field) { // Make sure we have field type before proceeding $field['type'] = isset($field['type']) ? $field['type'] : 'text'; // Sanitize values if ($field['type'] == 'checkbox') { $_POST[$key] = isset($_POST[$key]) ? 1 : 0; } else { $_POST[$key] = isset($_POST[$key]) ? wc_clean($_POST[$key]) : ''; } // Required field empty? if (!empty($field['required']) && empty($_POST[$key])) { wc_add_notice($field['label'] . ' ' . __('is a required field.', 'subscriptio'), 'error'); } // Validate field according to rules if (!empty($field['validate']) && is_array($field['validate'])) { foreach ($field['validate'] as $rule) { if ($rule == 'postcode') { $_POST[$key] = strtoupper(str_replace(' ', '', $_POST[$key])); if (WC_Validation::is_postcode($_POST[$key], $_POST['shipping_country'])) { $_POST[$key] = wc_format_postcode($_POST[$key], $_POST['shipping_country']); } else { wc_add_notice(__('Please enter a valid postcode/ZIP.', 'subscriptio'), 'error'); } } else if ($rule == 'phone') { $_POST[$key] = wc_format_phone_number($_POST[$key]); if (!WC_Validation::is_phone($_POST[$key])) { wc_add_notice('<strong>' . $field['label'] . '</strong> ' . __('is not a valid phone number.', 'subscriptio'), 'error'); } } else if ($rule == 'email') { $_POST[$key] = strtolower($_POST[$key]); if (!is_email($_POST[$key])) { wc_add_notice('<strong>' . $field['label'] . '</strong> ' . __('is not a valid email address.', 'woocommerce'), 'error'); } } } } } // No errors in form? if (wc_notice_count('error') == 0) { // Try to save address if ($subscription->update_shipping_address($_POST, true, true)) { wc_add_notice(__('Shipping address has been updated.', 'subscriptio')); } // Something went really wrong... else { wc_add_notice(__('Something went wrong...', 'subscriptio'), 'error'); } // Redirect to subscription page self::redirect_to_subscription($subscription); } else { self::display_address_form($subscription); } } // Display form else { self::display_address_form($subscription); } } }
/** * Save and and update a billing or shipping address if the * form was submitted through the user account page. */ public static function save_address() { global $wp; if ('POST' !== strtoupper($_SERVER['REQUEST_METHOD'])) { return; } if (empty($_POST['action']) || 'edit_address' !== $_POST['action'] || empty($_POST['_wpnonce']) || !wp_verify_nonce($_POST['_wpnonce'], 'woocommerce-edit_address')) { return; } $user_id = get_current_user_id(); if ($user_id <= 0) { return; } $load_address = isset($wp->query_vars['edit-address']) ? wc_edit_address_i18n(sanitize_title($wp->query_vars['edit-address']), true) : 'billing'; $address = WC()->countries->get_address_fields(esc_attr($_POST[$load_address . '_country']), $load_address . '_'); foreach ($address as $key => $field) { if (!isset($field['type'])) { $field['type'] = 'text'; } // Get Value. switch ($field['type']) { case 'checkbox': $_POST[$key] = (int) isset($_POST[$key]); break; default: $_POST[$key] = isset($_POST[$key]) ? wc_clean($_POST[$key]) : ''; break; } // Hook to allow modification of value. $_POST[$key] = apply_filters('woocommerce_process_myaccount_field_' . $key, $_POST[$key]); // Validation: Required fields. if (!empty($field['required']) && empty($_POST[$key])) { wc_add_notice(sprintf(__('%s is a required field.', 'woocommerce'), $field['label']), 'error'); } if (!empty($_POST[$key])) { // Validation rules. if (!empty($field['validate']) && is_array($field['validate'])) { foreach ($field['validate'] as $rule) { switch ($rule) { case 'postcode': $_POST[$key] = strtoupper(str_replace(' ', '', $_POST[$key])); if (!WC_Validation::is_postcode($_POST[$key], $_POST[$load_address . '_country'])) { wc_add_notice(__('Please enter a valid postcode / ZIP.', 'woocommerce'), 'error'); } else { $_POST[$key] = wc_format_postcode($_POST[$key], $_POST[$load_address . '_country']); } break; case 'phone': $_POST[$key] = wc_format_phone_number($_POST[$key]); if (!WC_Validation::is_phone($_POST[$key])) { wc_add_notice(sprintf(__('%s is not a valid phone number.', 'woocommerce'), '<strong>' . $field['label'] . '</strong>'), 'error'); } break; case 'email': $_POST[$key] = strtolower($_POST[$key]); if (!is_email($_POST[$key])) { wc_add_notice(sprintf(__('%s is not a valid email address.', 'woocommerce'), '<strong>' . $field['label'] . '</strong>'), 'error'); } break; } } } } } do_action('woocommerce_after_save_address_validation', $user_id, $load_address, $address); if (0 === wc_notice_count('error')) { foreach ($address as $key => $field) { update_user_meta($user_id, $key, $_POST[$key]); } wc_add_notice(__('Address changed successfully.', 'woocommerce')); do_action('woocommerce_customer_save_address', $user_id, $load_address); wp_safe_redirect(wc_get_endpoint_url('edit-address', '', wc_get_page_permalink('myaccount'))); exit; } }
/** * Get customer's shipping postcode. * @return string */ public function get_shipping_postcode() { return wc_format_postcode($this->data['shipping']['postcode'], $this->get_shipping_country()); }
/** * Validates the posted checkout data based on field properties. * * @since 2.7.0 * @param array $data An array of posted data. * @param WP_Error $errors */ protected function validate_posted_data(&$data, &$errors) { foreach ($this->get_checkout_fields() as $fieldset_key => $fieldset) { if ($this->maybe_skip_fieldset($fieldset_key, $data)) { continue; } foreach ($fieldset as $key => $field) { if (!isset($data[$key])) { continue; } $required = !empty($field['required']); $format = array_filter(isset($field['validate']) ? (array) $field['validate'] : array()); $field_label = isset($field['label']) ? $field['label'] : ''; switch ($fieldset_key) { case 'shipping': /* translators: %s: field name */ $field_label = sprintf(__('Shipping %s', 'woocommerce'), strtolower($field_label)); break; case 'billing': /* translators: %s: field name */ $field_label = sprintf(__('Billing %s', 'woocommerce'), strtolower($field_label)); break; } if (in_array('postcode', $format)) { $country = isset($data[$fieldset_key . '_country']) ? $data[$fieldset_key . '_country'] : WC()->customer->{"get_{$fieldset_key}_country"}(); $data[$key] = wc_format_postcode($data[$key], $country); if ('' !== $data[$key] && !WC_Validation::is_postcode($data[$key], $country)) { $errors->add('validation', __('Please enter a valid postcode / ZIP.', 'woocommerce')); } } if (in_array('phone', $format)) { $data[$key] = wc_format_phone_number($data[$key]); if ('' !== $data[$key] && !WC_Validation::is_phone($data[$key])) { /* translators: %s: phone number */ $errors->add('validation', sprintf(__('%s is not a valid phone number.', 'woocommerce'), '<strong>' . $field_label . '</strong>')); } } if (in_array('email', $format)) { $data[$key] = sanitize_email($data[$key]); if ('' !== $data[$key] && !is_email($data[$key])) { /* translators: %s: email address */ $errors->add('validation', sprintf(__('%s is not a valid email address.', 'woocommerce'), '<strong>' . $field_label . '</strong>')); } } if ('' !== $data[$key] && in_array('state', $format)) { $country = isset($data[$fieldset_key . '_country']) ? $data[$fieldset_key . '_country'] : WC()->customer->{"get_{$fieldset_key}_country"}(); $valid_states = WC()->countries->get_states($country); if (!empty($valid_states) && is_array($valid_states) && sizeof($valid_states) > 0) { $valid_state_values = array_flip(array_map('strtolower', $valid_states)); // Convert value to key if set if (isset($valid_state_values[strtolower($data[$key])])) { $data[$key] = $valid_state_values[strtolower($data[$key])]; } if (!in_array($data[$key], array_keys($valid_states))) { /* translators: 1: state field 2: valid states */ $errors->add('validation', sprintf(__('%1$s is not valid. Please enter one of the following: %2$s', 'woocommerce'), '<strong>' . $field_label . '</strong>', implode(', ', $valid_states))); } } } if ($required && '' === $data[$key]) { /* translators: %s: field name */ $errors->add('required-field', apply_filters('woocommerce_checkout_required_field_notice', sprintf(__('%s is a required field.', 'woocommerce'), '<strong>' . $field_label . '</strong>'), $field_label)); } } } }