Merge pull request #15652 from woocommerce/fix/15632

Allow state field to have the correct country set by passing it through
This commit is contained in:
Claudiu Lodromanean 2017-06-16 10:58:40 -07:00 committed by GitHub
commit 4d25ec6296
6 changed files with 52 additions and 20 deletions

View File

@ -659,10 +659,10 @@ class WC_Countries {
'required' => false,
'hidden' => true,
),
'state' => array(
'required' => false,
'state' => array(
'required' => false,
),
),
),
'AF' => array(
'state' => array(
'required' => false,
@ -1113,6 +1113,9 @@ class WC_Countries {
$address_fields = array();
foreach ( $fields as $key => $value ) {
if ( 'state' === $key ) {
$value['country_field'] = $type . 'country';
}
$address_fields[ $type . $key ] = $value;
}

View File

@ -1680,3 +1680,15 @@ function wc_make_phone_clickable( $phone ) {
return '<a href="tel:' . esc_attr( $number ) . '">' . esc_html( $phone ) . '</a>';
}
/**
* Get an item of post data if set, otherwise return a default value.
*
* @since 3.0.9
* @param string $key
* @param string $default
* @return mixed value sanitized by wc_clean
*/
function wc_get_post_data_by_key( $key, $default = '' ) {
return wc_clean( isset( $_POST[ $key ] ) ? $_POST[ $key ] : $default );
}

View File

@ -1898,7 +1898,7 @@ if ( ! function_exists( 'woocommerce_form_field' ) ) {
* @subpackage Forms
*
* @param string $key
* @param mixed $args
* @param mixed $args
* @param string $value (default: null)
*
* @return string
@ -2003,11 +2003,9 @@ if ( ! function_exists( 'woocommerce_form_field' ) ) {
break;
case 'state' :
/* Get Country */
$country_key = 'billing_state' === $key ? 'billing_country' : 'shipping_country';
$current_cc = WC()->checkout->get_value( $country_key );
$states = WC()->countries->get_states( $current_cc );
/* Get country this state field is representing */
$for_country = isset( $args['country'] ) ? $args['country'] : WC()->checkout->get_value( 'billing_state' === $key ? 'billing_country' : 'shipping_country' );
$states = WC()->countries->get_states( $for_country );
if ( is_array( $states ) && empty( $states ) ) {
@ -2015,7 +2013,7 @@ if ( ! function_exists( 'woocommerce_form_field' ) ) {
$field .= '<input type="hidden" class="hidden" name="' . esc_attr( $key ) . '" id="' . esc_attr( $args['id'] ) . '" value="" ' . implode( ' ', $custom_attributes ) . ' placeholder="' . esc_attr( $args['placeholder'] ) . '" />';
} elseif ( ! is_null( $current_cc ) && is_array( $states ) ) {
} elseif ( ! is_null( $for_country ) && is_array( $states ) ) {
$field .= '<select name="' . esc_attr( $key ) . '" id="' . esc_attr( $args['id'] ) . '" class="state_select ' . esc_attr( implode( ' ', $args['input_class'] ) ) . '" ' . implode( ' ', $custom_attributes ) . ' data-placeholder="' . esc_attr( $args['placeholder'] ) . '">
<option value="">' . esc_html__( 'Select a state&hellip;', 'woocommerce' ) . '</option>';

View File

@ -37,9 +37,16 @@ if ( ! defined( 'ABSPATH' ) ) {
<?php do_action( 'woocommerce_before_checkout_billing_form', $checkout ); ?>
<div class="woocommerce-billing-fields__field-wrapper">
<?php foreach ( $checkout->get_checkout_fields( 'billing' ) as $key => $field ) : ?>
<?php woocommerce_form_field( $key, $field, $checkout->get_value( $key ) ); ?>
<?php endforeach; ?>
<?php
$fields = $checkout->get_checkout_fields( 'billing' );
foreach ( $fields as $key => $field ) {
if ( isset( $field['country_field'], $fields[ $field['country_field'] ] ) ) {
$field['country'] = $checkout->get_value( $field['country_field'] );
}
woocommerce_form_field( $key, $field, $checkout->get_value( $key ) );
}
?>
</div>
<?php do_action( 'woocommerce_after_checkout_billing_form', $checkout ); ?>

View File

@ -35,9 +35,16 @@ if ( ! defined( 'ABSPATH' ) ) {
<?php do_action( 'woocommerce_before_checkout_shipping_form', $checkout ); ?>
<div class="woocommerce-shipping-fields__field-wrapper">
<?php foreach ( $checkout->get_checkout_fields( 'shipping' ) as $key => $field ) : ?>
<?php woocommerce_form_field( $key, $field, $checkout->get_value( $key ) ); ?>
<?php endforeach; ?>
<?php
$fields = $checkout->get_checkout_fields( 'shipping' );
foreach ( $fields as $key => $field ) {
if ( isset( $field['country_field'], $fields[ $field['country_field'] ] ) ) {
$field['country'] = $checkout->get_value( $field['country_field'] );
}
woocommerce_form_field( $key, $field, $checkout->get_value( $key ) );
}
?>
</div>
<?php do_action( 'woocommerce_after_checkout_shipping_form', $checkout ); ?>
@ -58,7 +65,7 @@ if ( ! defined( 'ABSPATH' ) ) {
<?php endif; ?>
<div class="woocommerce-additional-fields__field-wrapper">
<?php foreach ( $checkout->get_checkout_fields( 'order' ) as $key => $field ) : ?>
<?php foreach ( $checkout->get_checkout_fields( 'order' ) as $key => $field ) : ?>
<?php woocommerce_form_field( $key, $field, $checkout->get_value( $key ) ); ?>
<?php endforeach; ?>
</div>

View File

@ -36,9 +36,14 @@ do_action( 'woocommerce_before_edit_account_address_form' ); ?>
<?php do_action( "woocommerce_before_edit_address_form_{$load_address}" ); ?>
<div class="woocommerce-address-fields__field-wrapper">
<?php foreach ( $address as $key => $field ) : ?>
<?php woocommerce_form_field( $key, $field, ! empty( $_POST[ $key ] ) ? wc_clean( $_POST[ $key ] ) : $field['value'] ); ?>
<?php endforeach; ?>
<?php
foreach ( $address as $key => $field ) {
if ( isset( $field['country_field'], $address[ $field['country_field'] ] ) ) {
$field['country'] = wc_get_post_data_by_key( $field['country_field'], $address[ $field['country_field'] ]['value'] );
}
woocommerce_form_field( $key, $field, wc_get_post_data_by_key( $key, $field['value'] ) );
}
?>
</div>
<?php do_action( "woocommerce_after_edit_address_form_{$load_address}" ); ?>