WooCommerce NON EU VAT Message On Checkout - Changing Code To Include An Array Instead Of Single Value
I'm using a modified snippet from BBloomer on my checkout.
I looks for the country selected, and if country is Norway 'NO', it echos a message after the total amount regarding TAX info for Non EU countries.
As we now ship to UK and Switcherland as well, I want tom include those countries in mu script.
That means that the var countryCode = 'NO'; should be something like var countryCode = array( 'NO','UK','CH' ); or ?
Rest of the mighty also need to handle an array instead of the single value.
Hope anybody can help.
// The message
add_action( 'woocommerce_review_order_after_order_total', 'bbloomer_echo_notice_shipping' );
function bbloomer_echo_notice_shipping() {
echo '<tr class="non-eu-tax-notice" style="display:none">
<th>'. __( 'Notice', 'woocommerce' ) .'</th>
<td data-title=" '. __( 'Notice', 'woocommerce' ) .' ">'. __( 'No VAT charged. Please be aware that VAT and customs can be declared in your home country. More info here', 'woocommerce' ) .'</td>
</tr>';
}
// Show or hide message based on billing country
add_action( 'woocommerce_checkout_after_order_review', 'bbloomer_show_notice_shipping' );
function bbloomer_show_notice_shipping(){
wc_enqueue_js( "
// Set the country code that will display the message
var countryCode = 'NO';
// Get country code from checkout
selectedCountry = $('select#billing_country').val();
// Function to toggle message
function toggle_upsell( selectedCountry ) {
if( selectedCountry == countryCode ){
$('.non-eu-tax-notice').show();
}
else {
$('.non-eu-tax-notice').hide();
}
}
// Call function
toggle_upsell( selectedCountry );
$('select#billing_country').change(function(){
toggle_upsell( this.value );
});
" );
}
Answer
I can help! :D
I'm the WooCommerce: Show Message Upon Country Selection @ Checkout tutorial author.
In jQuery/JS, you can define an array:
var countryCode = [ 'NO', 'GB', 'CH' ];
Also, you can use a JS equivalent for PHP's in_array() which is inArray() to see if an element is inside the defined list of countries:
if ( $.inArray(selectedCountry,countryCode) !== -1 ){
Final code:
function bbloomer_show_notice_shipping(){
wc_enqueue_js( "
// Set the country code that will display the message
var countryCode = [ 'NO', 'GB', 'CH' ];
// Get country code from checkout
selectedCountry = $('select#billing_country').val();
// Function to toggle message
function toggle_upsell( selectedCountry ) {
if ( $.inArray(selectedCountry,countryCode) !== -1 ){
$('.non-eu-tax-notice').show();
}
else {
$('.non-eu-tax-notice').hide();
}
}
// Call function
toggle_upsell( selectedCountry );
$('select#billing_country').change(function(){
toggle_upsell( this.value );
});
" );
}
Related Questions
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS create a multi select Form field
- → October CMS - Conditionally Load a Different Page
- → How to disable assets combining on development in OctoberCMS
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → OctoberCms component: How to display all ID(items) instead of sorting only one ID?
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?