Update tax rates upon store location change.

This commit is contained in:
Jeff Stieler 2017-05-29 11:43:56 -06:00 committed by Nabeel Sulieman
parent 86f6921ced
commit 466cee9fc9
2 changed files with 55 additions and 56 deletions

View File

@ -4,24 +4,57 @@ jQuery( function( $ ) {
var locale_info = $.parseJSON( wc_setup_params.locale_info ); var locale_info = $.parseJSON( wc_setup_params.locale_info );
$( 'select[name="store_location"]' ).change( function() { $( 'select[name="store_location"]' ).change( function() {
var country_option = $(this).val(); var country_option = $(this).val().split( ':' );
var country = country_option.split( ':' )[0]; var country = country_option[0];
var state = country_option[1] || false;
var country_locale_info = locale_info[ country ]; var country_locale_info = locale_info[ country ];
var hide_if_set = [ 'thousand_sep', 'decimal_sep', 'num_decimals', 'currency_pos' ]; var hide_if_set = [ 'thousand_sep', 'decimal_sep', 'num_decimals', 'currency_pos' ];
if ( country_locale_info ) { if ( country_locale_info ) {
$.each( country_locale_info, function( index, value) { $.each( country_locale_info, function( index, value ) {
$(':input[name="' + index + '"]').val( value ).change(); $(':input[name="' + index + '"]').val( value ).change();
if ( -1 !== $.inArray( index, hide_if_set ) ) { if ( -1 !== $.inArray( index, hide_if_set ) ) {
$(':input[name="' + index + '"]').closest('tr').hide(); $(':input[name="' + index + '"]').closest('tr').hide();
} }
} ); } );
if ( ! $.isArray( country_locale_info.tax_rates ) ) {
var tax_rates = [];
if ( state && country_locale_info.tax_rates[ state ] ) {
tax_rates = tax_rates.concat( country_locale_info.tax_rates[ state ] );
} else if ( country_locale_info.tax_rates[ '' ] ) {
tax_rates = tax_rates.concat( country_locale_info.tax_rates[ '' ] );
}
tax_rates = tax_rates.concat( country_locale_info.tax_rates[ '*' ] || [] );
if ( tax_rates.length ) {
var $rate_table = $( 'table.tax-rates tbody' ).empty();
$.each( tax_rates, function( index, rate ) {
$( '<tr>', {
html: [
$( '<td>', { class: 'readonly', text: rate.country || '' } ),
$( '<td>', { class: 'readonly', text: rate.state || '*' } ),
$( '<td>', { class: 'readonly', text: rate.rate || '' } ),
$( '<td>', { class: 'readonly', text: rate.name || '' } )
]
} ).appendTo( $rate_table );
} );
$( '.tax-rates' ).show();
} else {
$( '.tax-rates' ).hide();
}
}
} else { } else {
$(':input[name="currency_pos"]').closest('tr').show(); $(':input[name="currency_pos"]').closest('tr').show();
$(':input[name="thousand_sep"]').closest('tr').show(); $(':input[name="thousand_sep"]').closest('tr').show();
$(':input[name="decimal_sep"]').closest('tr').show(); $(':input[name="decimal_sep"]').closest('tr').show();
$(':input[name="num_decimals"]').closest('tr').show(); $(':input[name="num_decimals"]').closest('tr').show();
$( '.tax-rates' ).hide();
} }
} ).change(); } ).change();

View File

@ -388,59 +388,25 @@ class WC_Admin_Setup_Wizard {
<label><input type="radio" <?php checked( get_option( 'woocommerce_prices_include_tax', 'no' ), 'no' ); ?> id="woocommerce_prices_include_tax" name="woocommerce_prices_include_tax" class="input-radio" value="no" /> <?php esc_html_e( 'I will enter prices exclusive of tax', 'woocommerce' ); ?></label> <label><input type="radio" <?php checked( get_option( 'woocommerce_prices_include_tax', 'no' ), 'no' ); ?> id="woocommerce_prices_include_tax" name="woocommerce_prices_include_tax" class="input-radio" value="no" /> <?php esc_html_e( 'I will enter prices exclusive of tax', 'woocommerce' ); ?></label>
</td> </td>
</tr> </tr>
<?php <tr class="tax-rates" style="display: none;">
$locale_info = include( WC()->plugin_path() . '/i18n/locale-info.php' ); <td colspan="2">
$tax_rates = array(); <p><?php printf( __( 'The following tax rates will be imported automatically for you. You can read more about taxes in <a href="%s" target="_blank">our documentation</a>.', 'woocommerce' ), 'https://docs.woocommerce.com/document/setting-up-taxes-in-woocommerce/' ); ?></p>
$country = WC()->countries->get_base_country(); <div class="importing-tax-rates">
$state = WC()->countries->get_base_state(); <table class="tax-rates">
<thead>
if ( isset( $locale_info[ $country ] ) ) { <tr>
if ( isset( $locale_info[ $country ]['tax_rates'][ $state ] ) ) { <th><?php esc_html_e( 'Country', 'woocommerce' ); ?></th>
$tax_rates = $locale_info[ $country ]['tax_rates'][ $state ]; <th><?php esc_html_e( 'State', 'woocommerce' ); ?></th>
} elseif ( isset( $locale_info[ $country ]['tax_rates'][''] ) ) { <th><?php esc_html_e( 'Rate (%)', 'woocommerce' ); ?></th>
$tax_rates = $locale_info[ $country ]['tax_rates']['']; <th><?php esc_html_e( 'Name', 'woocommerce' ); ?></th>
} </tr>
if ( isset( $locale_info[ $country ]['tax_rates']['*'] ) ) { </thead>
$tax_rates = array_merge( $locale_info[ $country ]['tax_rates']['*'], $tax_rates ); <tbody></tbody>
} </table>
} </div>
if ( $tax_rates ) { <p class="description"><?php printf( __( 'You may need to add/edit rates based on your products or business location which can be done from the <a href="%s" target="_blank">tax settings</a> screen. If in doubt, speak to an accountant.', 'woocommerce' ), esc_url( admin_url( 'admin.php?page=wc-settings&tab=tax' ) ) ); ?></p>
?> </td>
<tr class="tax-rates"> </tr>
<td colspan="2">
<p><?php printf( __( 'The following tax rates will be imported automatically for you. You can read more about taxes in <a href="%s" target="_blank">our documentation</a>.', 'woocommerce' ), 'https://docs.woocommerce.com/document/setting-up-taxes-in-woocommerce/' ); ?></p>
<div class="importing-tax-rates">
<table class="tax-rates">
<thead>
<tr>
<th><?php esc_html_e( 'Country', 'woocommerce' ); ?></th>
<th><?php esc_html_e( 'State', 'woocommerce' ); ?></th>
<th><?php esc_html_e( 'Rate (%)', 'woocommerce' ); ?></th>
<th><?php esc_html_e( 'Name', 'woocommerce' ); ?></th>
</tr>
</thead>
<tbody>
<?php
foreach ( $tax_rates as $rate ) {
?>
<tr>
<td class="readonly"><?php echo esc_attr( $rate['country'] ); ?></td>
<td class="readonly"><?php echo esc_attr( $rate['state'] ? $rate['state'] : '*' ); ?></td>
<td class="readonly"><?php echo esc_attr( $rate['rate'] ); ?></td>
<td class="readonly"><?php echo esc_attr( $rate['name'] ); ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
<p class="description"><?php printf( __( 'You may need to add/edit rates based on your products or business location which can be done from the <a href="%s" target="_blank">tax settings</a> screen. If in doubt, speak to an accountant.', 'woocommerce' ), esc_url( admin_url( 'admin.php?page=wc-settings&tab=tax' ) ) ); ?></p>
</td>
</tr>
<?php
}
?>
</table> </table>
<p class="wc-setup-actions step"> <p class="wc-setup-actions step">
<input type="submit" class="button-primary button button-large button-next" value="<?php esc_attr_e( 'Continue', 'woocommerce' ); ?>" name="save_step" /> <input type="submit" class="button-primary button button-large button-next" value="<?php esc_attr_e( 'Continue', 'woocommerce' ); ?>" name="save_step" />