本文整理汇总了PHP中wc_format_postcode函数的典型用法代码示例。如果您正苦于以下问题:PHP wc_format_postcode函数的具体用法?PHP wc_format_postcode怎么用?PHP wc_format_postcode使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了wc_format_postcode函数的18个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的PHP代码示例。
示例1: is_valid_postcode
/**
* 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;
}
开发者ID:unfulvio,项目名称:woocommerce,代码行数:32,代码来源:class-wc-shipping-local-pickup.php
示例2: 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');
}
}
}
开发者ID:CannedHead,项目名称:feelingsurf,代码行数:32,代码来源:class-wc-shortcode-cart.php
示例3: 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');
}
}
开发者ID:chhavinav,项目名称:fr.ilovejuice,代码行数:54,代码来源:class-wc-shortcode-cart.php
示例4: format_postcode
/**
* 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);
}
开发者ID:Joaquinsemp,项目名称:patriestausado,代码行数:11,代码来源:class-wc-validation.php
示例5: get_shipping_postcode
/**
* 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());
}
开发者ID:nightbook,项目名称:woocommerce,代码行数:9,代码来源:class-wc-customer.php
示例6: 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');
}
//.........这里部分代码省略.........
开发者ID:Joaquinsemp,项目名称:patriestausado,代码行数:101,代码来源:class-wc-checkout.php
示例7: 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()
}
}
}
开发者ID:jgabrielfreitas,项目名称:MultipagosTestesAPP,代码行数:38,代码来源:class-wc-subscriptions-cart.php
示例8: is_available
/**
* 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);
}
开发者ID:uwitec,项目名称:findgreatmaster,代码行数:64,代码来源:class-wc-shipping-local-pickup.php
示例9: wc_get_wildcard_postcodes
/**
* 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;
}
开发者ID:WPprodigy,项目名称:woocommerce,代码行数:18,代码来源:wc-core-functions.php
示例10: get_shipping_postcode
/**
* 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());
}
}
开发者ID:Joaquinsemp,项目名称:patriestausado,代码行数:12,代码来源:class-wc-customer.php
示例11: get_woocommerce_zipcode
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;
}
开发者ID:brasadesign,项目名称:aboaterra-theme,代码行数:8,代码来源:class-check-delivery.php
示例12: test_wc_format_postcode
/**
* 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'));
}
开发者ID:jimlove7273,项目名称:woocommerce,代码行数:12,代码来源:functions.php
示例13: 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) {
//.........这里部分代码省略.........
开发者ID:RainyDayMedia,项目名称:carbide-probes,代码行数:101,代码来源:woocommerce-shipping-multiple-address.php
示例14: 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);
}
开发者ID:slavic18,项目名称:cats,代码行数:50,代码来源:class-wc-subscriptions-cart.php
示例15: 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);
}
}
}
开发者ID:qhuit,项目名称:dcosta,代码行数:96,代码来源:subscriptio-my-account.class.php
示例16: 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;
}
}
开发者ID:johnulist,项目名称:woocommerce,代码行数:79,代码来源:class-wc-form-handler.php
示例17: get_shipping_postcode
/**
* Get customer's shipping postcode.
* @return string
*/
public function get_shipping_postcode()
{
return wc_format_postcode($this->data['shipping']['postcode'], $this->get_shipping_country());
}
开发者ID:tlovett1,项目名称:woocommerce,代码行数:8,代码来源:class-wc-customer.php
示例18: 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', $
|
请发表评论