Customer profile: Add a “Copy from billing” button to shipping user meta fields

This commit is contained in:
Kelly Dwan 2017-05-15 17:09:02 -04:00
parent 05132f724c
commit faea811a92
2 changed files with 30 additions and 2 deletions

View File

@ -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' );
} );
}
};

View File

@ -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 ) :
?>
@ -171,6 +178,8 @@ class WC_Admin_Profile {
</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 ); ?> />
<?php elseif ( ! empty( $field['type'] ) && 'button' === $field['type'] ) : ?>
<button id="<?php echo esc_attr( $key ); ?>" class="button <?php echo ( ! empty( $field['class'] ) ? $field['class'] : '' ); ?>"><?php echo esc_attr( $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' ); ?>" />
<?php endif; ?>