Merge pull request #15084 from ryelle/add/profile-field-improvements
Customer Profiles: copy billing address to shipping address
This commit is contained in:
commit
34a7f9b3dd
|
@ -15,6 +15,8 @@ jQuery( function ( $ ) {
|
|||
$( '.js_field-country' ).select2().change( this.change_country );
|
||||
$( '.js_field-country' ).trigger( 'change', [ true ] );
|
||||
$( document.body ).on( 'change', 'select.js_field-state', this.change_state );
|
||||
|
||||
$( document.body ).on( 'click', 'button.js_copy-billing', this.copy_billing );
|
||||
},
|
||||
|
||||
change_country: function( e, stickValue ) {
|
||||
|
@ -75,6 +77,23 @@ jQuery( function ( $ ) {
|
|||
country = $country.val();
|
||||
|
||||
$country.data( 'woocommerce.stickState-' + country, state );
|
||||
},
|
||||
|
||||
copy_billing: function( event ) {
|
||||
event.preventDefault();
|
||||
|
||||
$( '#fieldset-billing' ).find( 'input, select' ).each( function( i, el ) {
|
||||
// The address keys match up, except for the prefix
|
||||
var shipName = el.name.replace( /^billing_/, 'shipping_' );
|
||||
// Swap prefix, then check if there are any elements
|
||||
var shipEl = $( '[name="' + shipName + '"]' );
|
||||
// No corresponding shipping field, skip this item
|
||||
if ( ! shipEl.length ) {
|
||||
return;
|
||||
}
|
||||
// Found a matching shipping element, update the value
|
||||
shipEl.val( el.value ).trigger( 'change' );
|
||||
} );
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -1 +1 @@
|
|||
jQuery(function(a){var b={states:null,init:function(){"undefined"!=typeof wc_users_params.countries&&(this.states=a.parseJSON(wc_users_params.countries.replace(/"/g,'"'))),a(".js_field-country").select2().change(this.change_country),a(".js_field-country").trigger("change",[!0]),a(document.body).on("change","select.js_field-state",this.change_state)},change_country:function(c,d){if("undefined"==typeof d&&(d=!1),null!==b.states){var e=a(this),f=e.val(),g=e.parents(".form-table").find(":input.js_field-state"),h=g.parent(),i=g.attr("name"),j=g.attr("id"),k=e.data("woocommerce.stickState-"+f)?e.data("woocommerce.stickState-"+f):g.val();if(d&&e.data("woocommerce.stickState-"+f,k),h.show().find(".select2-container").remove(),a.isEmptyObject(b.states[f]))g.replaceWith('<input type="text" class="js_field-state" name="'+i+'" id="'+j+'" value="'+k+'" />');else{var l=a('<select name="'+i+'" id="'+j+'" class="js_field-state" style="width: 25em;"></select>'),m=b.states[f];l.append(a('<option value="">'+wc_users_params.i18n_select_state_text+"</option>")),a.each(m,function(b){l.append(a('<option value="'+b+'">'+m[b]+"</option>"))}),l.val(k),g.replaceWith(l),l.show().select2().hide().change()}a(document.body).trigger("contry-change.woocommerce",[f,a(this).closest("div")]),a(document.body).trigger("country-change.woocommerce",[f,a(this).closest("div")])}},change_state:function(){var b=a(this),c=b.val(),d=b.parents(".form-table").find(":input.js_field-country"),e=d.val();d.data("woocommerce.stickState-"+e,c)}};b.init()});
|
||||
jQuery(function(a){var b={states:null,init:function(){"undefined"!=typeof wc_users_params.countries&&(this.states=a.parseJSON(wc_users_params.countries.replace(/"/g,'"'))),a(".js_field-country").select2().change(this.change_country),a(".js_field-country").trigger("change",[!0]),a(document.body).on("change","select.js_field-state",this.change_state),a(document.body).on("click","button.js_copy-billing",this.copy_billing)},change_country:function(c,d){if("undefined"==typeof d&&(d=!1),null!==b.states){var e=a(this),f=e.val(),g=e.parents(".form-table").find(":input.js_field-state"),h=g.parent(),i=g.attr("name"),j=g.attr("id"),k=e.data("woocommerce.stickState-"+f)?e.data("woocommerce.stickState-"+f):g.val();if(d&&e.data("woocommerce.stickState-"+f,k),h.show().find(".select2-container").remove(),a.isEmptyObject(b.states[f]))g.replaceWith('<input type="text" class="js_field-state" name="'+i+'" id="'+j+'" value="'+k+'" />');else{var l=a('<select name="'+i+'" id="'+j+'" class="js_field-state" style="width: 25em;"></select>'),m=b.states[f];l.append(a('<option value="">'+wc_users_params.i18n_select_state_text+"</option>")),a.each(m,function(b){l.append(a('<option value="'+b+'">'+m[b]+"</option>"))}),l.val(k),g.replaceWith(l),l.show().select2().hide().change()}a(document.body).trigger("contry-change.woocommerce",[f,a(this).closest("div")]),a(document.body).trigger("country-change.woocommerce",[f,a(this).closest("div")])}},change_state:function(){var b=a(this),c=b.val(),d=b.parents(".form-table").find(":input.js_field-country"),e=d.val();d.data("woocommerce.stickState-"+e,c)},copy_billing:function(b){b.preventDefault(),a("#fieldset-billing").find("input, select").each(function(b,c){var d=c.name.replace(/^billing_/,"shipping_"),e=a('[name="'+d+'"]');e.length&&e.val(c.value).trigger("change")})}};b.init()});
|
|
@ -93,6 +93,13 @@ class WC_Admin_Profile {
|
|||
'shipping' => array(
|
||||
'title' => __( 'Customer shipping address', 'woocommerce' ),
|
||||
'fields' => array(
|
||||
'copy_billing' => array(
|
||||
'label' => __( 'Copy from billing address', 'woocommerce' ),
|
||||
'description' => '',
|
||||
'class' => 'js_copy-billing',
|
||||
'type' => 'button',
|
||||
'text' => __( 'Copy', 'woocommerce' ),
|
||||
),
|
||||
'shipping_first_name' => array(
|
||||
'label' => __( 'First name', 'woocommerce' ),
|
||||
'description' => '',
|
||||
|
@ -151,10 +158,10 @@ class WC_Admin_Profile {
|
|||
|
||||
$show_fields = $this->get_customer_meta_fields();
|
||||
|
||||
foreach ( $show_fields as $fieldset ) :
|
||||
foreach ( $show_fields as $fieldset_key => $fieldset ) :
|
||||
?>
|
||||
<h2><?php echo $fieldset['title']; ?></h2>
|
||||
<table class="form-table">
|
||||
<table class="form-table" id="<?php echo esc_attr( 'fieldset-' . $fieldset_key ); ?>">
|
||||
<?php
|
||||
foreach ( $fieldset['fields'] as $key => $field ) :
|
||||
?>
|
||||
|
@ -162,7 +169,7 @@ class WC_Admin_Profile {
|
|||
<th><label for="<?php echo esc_attr( $key ); ?>"><?php echo esc_html( $field['label'] ); ?></label></th>
|
||||
<td>
|
||||
<?php if ( ! empty( $field['type'] ) && 'select' === $field['type'] ) : ?>
|
||||
<select name="<?php echo esc_attr( $key ); ?>" id="<?php echo esc_attr( $key ); ?>" class="<?php echo ( ! empty( $field['class'] ) ? $field['class'] : '' ); ?>" style="width: 25em;">
|
||||
<select name="<?php echo esc_attr( $key ); ?>" id="<?php echo esc_attr( $key ); ?>" class="<?php echo esc_attr( $field['class'] ); ?>" style="width: 25em;">
|
||||
<?php
|
||||
$selected = esc_attr( get_user_meta( $user->ID, $key, true ) );
|
||||
foreach ( $field['options'] as $option_key => $option_value ) : ?>
|
||||
|
@ -170,9 +177,11 @@ class WC_Admin_Profile {
|
|||
<?php endforeach; ?>
|
||||
</select>
|
||||
<?php elseif ( ! empty( $field['type'] ) && 'checkbox' === $field['type'] ) : ?>
|
||||
<input type="checkbox" name="<?php echo esc_attr( $key ); ?>" id="<?php echo esc_attr( $key ); ?>" value="1" class="<?php echo ( ! empty( $field['class'] ) ? $field['class'] : '' ); ?>" <?php checked( (int) get_user_meta( $user->ID, $key, true ), 1, true ); ?> />
|
||||
<input type="checkbox" name="<?php echo esc_attr( $key ); ?>" id="<?php echo esc_attr( $key ); ?>" value="1" class="<?php echo esc_attr( $field['class'] ); ?>" <?php checked( (int) get_user_meta( $user->ID, $key, true ), 1, true ); ?> />
|
||||
<?php elseif ( ! empty( $field['type'] ) && 'button' === $field['type'] ) : ?>
|
||||
<button id="<?php echo esc_attr( $key ); ?>" class="button <?php echo esc_attr( $field['class'] ); ?>"><?php echo esc_html( $field['text'] ); ?></button>
|
||||
<?php else : ?>
|
||||
<input type="text" name="<?php echo esc_attr( $key ); ?>" id="<?php echo esc_attr( $key ); ?>" value="<?php echo esc_attr( get_user_meta( $user->ID, $key, true ) ); ?>" class="<?php echo ( ! empty( $field['class'] ) ? $field['class'] : 'regular-text' ); ?>" />
|
||||
<input type="text" name="<?php echo esc_attr( $key ); ?>" id="<?php echo esc_attr( $key ); ?>" value="<?php echo esc_attr( $this->get_user_meta( $user->ID, $key ) ); ?>" class="<?php echo ( ! empty( $field['class'] ) ? esc_attr( $field['class'] ) : 'regular-text' ); ?>" />
|
||||
<?php endif; ?>
|
||||
<br/>
|
||||
<span class="description"><?php echo wp_kses_post( $field['description'] ); ?></span>
|
||||
|
@ -206,6 +215,27 @@ class WC_Admin_Profile {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get user meta for a given key, with fallbacks to core user info for pre-existing fields.
|
||||
*
|
||||
* @since 3.1.0
|
||||
* @param int $user_id User ID of the user being edited
|
||||
* @param string $key Key for user meta field
|
||||
* @return string
|
||||
*/
|
||||
protected function get_user_meta( $user_id, $key ) {
|
||||
$value = get_user_meta( $user_id, $key, true );
|
||||
$existing_fields = array( 'billing_first_name', 'billing_last_name' );
|
||||
if ( ! $value && in_array( $key, $existing_fields ) ) {
|
||||
$value = get_user_meta( $user_id, str_replace( 'billing_', '', $key ), true );
|
||||
} elseif ( ! $value && ( 'billing_email' === $key ) ) {
|
||||
$user = get_userdata( $user_id );
|
||||
$value = $user->user_email;
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
||||
endif;
|
||||
|
|
Loading…
Reference in New Issue