本文整理汇总了PHP中WC_Validation::is_postcode方法的典型用法代码示例。如果您正苦于以下问题:PHP WC_Validation::is_postcode方法的具体用法?PHP WC_Validation::is_postcode怎么用?PHP WC_Validation::is_postcode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类WC_Validation
的用法示例。
在下文中一共展示了WC_Validation::is_postcode方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: calculate_shipping
/**
* 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');
}
}
}
示例2: checkUserPostcodeAction
public function checkUserPostcodeAction()
{
$country = $_POST['country'];
$postCode = $_POST['postCode'];
echo WC_Validation::is_postcode($postCode, $country);
exit;
}
开发者ID:likelazyeyes,项目名称:woocommerce-multistep-checkout-wizard-lite-moded,代码行数:7,代码来源:WooCheckoutStepsLiteFrontendFestiPlugin.php
示例3: gzd_revocation
/**
* Checks revocation form and sends Email to customer and Admin
*/
public static function gzd_revocation()
{
check_ajax_referer('woocommerce-revocation', 'security');
wp_verify_nonce($_POST['_wpnonce'], 'woocommerce-revocation');
$data = array();
$fields = WC_GZD_Revocation::get_fields();
if (!empty($fields)) {
foreach ($fields as $key => $field) {
if ('sep' !== $key) {
if ($key == 'address_mail') {
if (!is_email($_POST[$key])) {
wc_add_notice('<strong>' . $field['label'] . '</strong> ' . _x('is not a valid email address.', 'revocation-form', 'woocommerce-germanized'), 'error');
}
} elseif ($key == 'address_postal') {
if (!WC_Validation::is_postcode($_POST[$key], $_POST['address_country']) || empty($_POST[$key])) {
wc_add_notice(_x('Please enter a valid postcode/ZIP', 'revocation-form', 'woocommerce-germanized'), 'error');
}
} else {
if (isset($field['required']) && empty($_POST[$key])) {
wc_add_notice('<strong>' . $field['label'] . '</strong> ' . _x('is not valid.', 'revocation-form', 'woocommerce-germanized'), 'error');
}
}
if (!empty($_POST[$key])) {
if ($field['type'] == 'country') {
$countries = WC()->countries->get_countries();
$country = wc_clean($_POST[$key]);
$data[$key] = isset($countries[$country]) ? $countries[$country] : '';
} else {
$data[$key] = wc_clean($_POST[$key]);
}
}
}
}
}
$error = false;
if (wc_notice_count('error') == 0) {
wc_add_notice(_x('Thank you. We have received your Revocation Request. You will receive a conformation email within a few minutes.', 'revocation-form', 'woocommerce-germanized'), 'success');
// Send Mail
if ($mail = WC_germanized()->emails->get_email_instance_by_id('customer_revocation')) {
$mail->trigger($data);
// Send to Admin
$data['mail'] = get_bloginfo('admin_email');
$mail->trigger($data);
}
} else {
$error = true;
}
ob_start();
wc_print_notices();
$messages = ob_get_clean();
if ($error) {
echo '<!--WC_START-->' . json_encode(array('result' => 'failure', 'messages' => isset($messages) ? $messages : '')) . '<!--WC_END-->';
} else {
if (is_ajax()) {
echo '<!--WC_START-->' . json_encode(array('result' => 'success', 'messages' => isset($messages) ? $messages : '')) . '<!--WC_END-->';
}
}
exit;
}
示例4: data_provider_test_is_postcode
/**
* Data provider for test_is_postcode()
*
* @since 2.4
*/
public function data_provider_test_is_postcode()
{
$generic = array(array(true, \WC_Validation::is_postcode('99999', 'IT')), array(true, \WC_Validation::is_postcode('99999', 'IT')), array(true, \WC_Validation::is_postcode('9999', 'IT')), array(true, \WC_Validation::is_postcode('ABC 999', 'IT')), array(true, \WC_Validation::is_postcode('ABC-999', 'IT')), array(false, \WC_Validation::is_postcode('ABC_123', 'IT')));
$gb = array(array(true, \WC_Validation::is_postcode('A9 9AA', 'GB')), array(false, \WC_Validation::is_postcode('99999', 'GB')));
$us = array(array(true, \WC_Validation::is_postcode('99999', 'US')), array(true, \WC_Validation::is_postcode('99999-9999', 'US')), array(false, \WC_Validation::is_postcode('ABCDE', 'US')), array(false, \WC_Validation::is_postcode('ABCDE-9999', 'US')));
$ch = array(array(true, \WC_Validation::is_postcode('9999', 'CH')), array(false, \WC_Validation::is_postcode('99999', 'CH')), array(false, \WC_Validation::is_postcode('ABCDE', 'CH')));
$br = array(array(true, \WC_Validation::is_postcode('99999-999', 'BR')), array(true, \WC_Validation::is_postcode('99999999', 'BR')), array(false, \WC_Validation::is_postcode('99999 999', 'BR')), array(false, \WC_Validation::is_postcode('99999-ABC', 'BR')));
return array_merge($generic, $gb, $us, $ch, $br);
}
示例5: output
/**
* 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');
}
}
示例6: save_address
/**
* 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;
}
}
示例7: process_checkout
/**
* 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');
}
//.........这里部分代码省略.........
示例8: maybe_recalculate_shipping
/**
* 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()
}
}
}
示例9: validate_posted_data
/**
* 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));
}
}
}
}
示例10: is_valid_package_destination
/**
* Determine if a package's destination is valid enough for a rate quote.
*
* @param array $package
* @return bool
*/
public function is_valid_package_destination($package)
{
$country = isset($package['destination']['country']) ? $package['destination']['country'] : '';
$postcode = isset($package['destination']['postcode']) ? $package['destination']['postcode'] : '';
$state = isset($package['destination']['state']) ? $package['destination']['state'] : '';
// Ensure that Country is specified
if (empty($country)) {
return false;
}
// Validate Postcode
if (!WC_Validation::is_postcode($postcode, $country)) {
return false;
}
// Validate State
$valid_states = WC()->countries->get_states($country);
if ($valid_states && !array_key_exists($state, $valid_states)) {
return false;
}
return true;
}
示例11: require_billing_information
public static function require_billing_information($post_id, $post)
{
// must be shop order
if ($post->post_type != 'shop_order') {
return;
}
// only perform this check if the associated option is on
if (self::$options->{'qsot-require-billing-information'} != 'yes') {
return;
}
// only when the past is being saved in the admin
if (!isset($_POST['action']) || $_POST['action'] != 'editpost') {
return;
}
// load the order
$order = wc_get_order($post_id);
if (!is_object($order) || !isset($order->id)) {
return;
}
// do not perform this check on cancelled orders, because they are irrelevant checks at that point
if ('cancelled' == $order->get_status()) {
return;
}
// ****** most of this is adapted from the checkout logic from WC2.3.x
// get all the fields that we should be validating. derived from checkout process
$fields = WC()->countries->get_address_fields(self::_get_value('_billing_country', $order), '_billing_');
$errors = array();
// cycle through each field, and validate the input
foreach ($fields as $key => $field) {
// make sure we have a field type
if (!isset($field['type'])) {
$field['type'] = 'text';
}
// find the submitted value of the field
switch ($field['type']) {
// checkboxes are on or off
case 'checkbox':
$value = isset($_POST[$key]) ? 1 : 0;
break;
// multiselect boxes have multiple values that need cleaning
// multiselect boxes have multiple values that need cleaning
case 'multiselect':
$value = isset($_POST[$key]) ? implode(', ', array_map('wc_clean', $_POST[$key])) : '';
break;
// textareas allow for lots of text, so clean that up
// textareas allow for lots of text, so clean that up
case 'textarea':
$value = isset($_POST[$key]) ? wp_strip_all_tags(wp_check_invalid_utf8(stripslashes($_POST[$key]))) : '';
break;
// all other fields should be cleaned as well
// all other fields should be cleaned as well
default:
$value = isset($_POST[$key]) ? is_array($_POST[$key]) ? array_map('wc_clean', $_POST[$key]) : wc_clean($_POST[$key]) : '';
break;
}
// allow modification of resulting value
$value = apply_filters('woocommerce_process_checkout_' . sanitize_title($field['type']) . '_field', $value);
$value = apply_filters('woocommerce_process_checkout_field_' . $key, $value);
// check required fields
if (isset($field['required']) && $field['required'] && empty($value)) {
$error[] = '<strong>' . $field['label'] . '</strong> ' . __('is a required field.', 'woocommerce');
}
// some non-empty fields need addtiional validation. handle that here
if (!empty($value)) {
// cycle through the rules
if (isset($field['validate'])) {
foreach ($field['validate'] as $rule) {
// process each rule if it is in the list
switch ($rule) {
// postcodes vary from country to country
case 'postcode':
$value = strtoupper(str_replace(' ', '', $value));
if (!WC_Validation::is_postcode($value, $_POST[$key])) {
$errors[] = __('Please enter a valid postcode/ZIP.', 'woocommerce');
}
break;
// phone digit count and format varies from country to country
// phone digit count and format varies from country to country
case 'phone':
$value = wc_format_phone_number($value);
if (!WC_Validation::is_phone($value)) {
$errors[] = '<strong>' . $field['label'] . '</strong> ' . __('is not a valid phone number.', 'woocommerce');
}
break;
// validate email addresses
// validate email addresses
case 'email':
$value = strtolower($value);
if (!is_email($value)) {
$errors[] = '<strong>' . $field['label'] . '</strong> ' . __('is not a valid email address.', 'woocommerce');
}
break;
// states cound be in different formats or have different values based on the country
// states cound be in different formats or have different values based on the country
case 'state':
$states = WC()->countries->get_states(self::_get_value('_billing_country', $order));
if (!empty($states) && is_array($states)) {
$states = array_flip(array_map('strtolower', $states));
// look up correct value if key exists
if (isset($states[strtolower($value)])) {
//.........这里部分代码省略.........
示例12: save_address_book
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) {
//.........这里部分代码省略.........
示例13: maybe_recalculate_shipping
/**
* 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);
}
示例14: subscription_address
/**
* 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);
}
}
}
示例15: kco_iframe_change_cb
/**
* Pushes Klarna order update in AJAX calls.
*
* Used to capture customer address, recalculate tax and shipping for order and user session
*
* @since 2.0
**/
function kco_iframe_change_cb()
{
if (!wp_verify_nonce($_REQUEST['nonce'], 'klarna_checkout_nonce')) {
exit('Nonce can not be verified.');
}
global $woocommerce;
$data = array();
// Check stock
if (is_wp_error($woocommerce->cart->check_cart_item_stock())) {
wp_send_json_error();
wp_die();
}
// Capture email
if (isset($_REQUEST['email']) && is_string($_REQUEST['email']) && !is_user_logged_in()) {
$this->update_or_create_local_order($_REQUEST['email']);
$orderid = $woocommerce->session->get('ongoing_klarna_order');
$data['orderid'] = $orderid;
$connector = Klarna_Checkout_Connector::create($this->klarna_secret, $this->klarna_server);
$klarna_order = new Klarna_Checkout_Order($connector, WC()->session->get('klarna_checkout'));
$klarna_order->fetch();
$update['merchant']['push_uri'] = add_query_arg(array('sid' => $orderid), $klarna_order['merchant']['push_uri']);
$update['merchant']['confirmation_uri'] = add_query_arg(array('sid' => $orderid, 'order-received' => $orderid), $klarna_order['merchant']['confirmation_uri']);
$klarna_order->update($update);
}
// Capture postal code
if (isset($_REQUEST['postal_code']) && is_string($_REQUEST['postal_code']) && WC_Validation::is_postcode($_REQUEST['postal_code'], $this->klarna_country)) {
$woocommerce->customer->set_shipping_postcode($_REQUEST['postal_code']);
if (!defined('WOOCOMMERCE_CART')) {
define('WOOCOMMERCE_CART', true);
}
// Update user session
$woocommerce->cart->calculate_shipping();
$woocommerce->cart->calculate_fees();
$woocommerce->cart->calculate_totals();
// Update ongoing WooCommerce order
$this->update_or_create_local_order();
$data['widget_html'] = $this->klarna_checkout_get_kco_widget_html();
if (WC()->session->get('klarna_checkout')) {
$this->ajax_update_klarna_order();
}
}
wp_send_json_success($data);
wp_die();
}