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,8 +4,9 @@ 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' ];
@ -17,11 +18,43 @@ jQuery( function( $ ) {
$(':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,25 +388,7 @@ 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' );
$tax_rates = array();
$country = WC()->countries->get_base_country();
$state = WC()->countries->get_base_state();
if ( isset( $locale_info[ $country ] ) ) {
if ( isset( $locale_info[ $country ]['tax_rates'][ $state ] ) ) {
$tax_rates = $locale_info[ $country ]['tax_rates'][ $state ];
} elseif ( isset( $locale_info[ $country ]['tax_rates'][''] ) ) {
$tax_rates = $locale_info[ $country ]['tax_rates'][''];
}
if ( isset( $locale_info[ $country ]['tax_rates']['*'] ) ) {
$tax_rates = array_merge( $locale_info[ $country ]['tax_rates']['*'], $tax_rates );
}
}
if ( $tax_rates ) {
?>
<tr class="tax-rates">
<td colspan="2"> <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> <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"> <div class="importing-tax-rates">
@ -419,28 +401,12 @@ class WC_Admin_Setup_Wizard {
<th><?php esc_html_e( 'Name', 'woocommerce' ); ?></th> <th><?php esc_html_e( 'Name', 'woocommerce' ); ?></th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody></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> </table>
</div> </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> <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> </td>
</tr> </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" />