Reputation: 163
In WooCommerce I use the following JS Code, to trigger the refresh fragment, when changing billing country:
jQuery(document).ready(function(){
// Change Billing Country: Refresh WC Fragment
jQuery(document.body).on('change', 'select[name=billing_country]', function(){
jQuery(document.body).trigger('wc_fragment_refresh');
});
});
This works perfectly and runs the following PHP function:
function wc_refresh_mini_cart_billing_country($fragments){
ob_start();
// Hidden code, works perfectly...
// Get and output billing country
$billing_country = WC()->customer->get_billing_country();
echo $billing_country;
// Update fragment
$fragments['#your-billing-country'] = ob_get_clean();
return $fragments;
}
add_filter( 'woocommerce_add_to_cart_fragments', 'wc_refresh_mini_cart_billing_country');
Now this works, BUT I always get the old billing country, not the current one. For example: I am on the checkout page and country is set to Germany. If I change that to France, the above code will display "DE" (for Germany), after changing it again to Italy, the above code will display "FR" (for France). So it always takes the last chosen country, not the current selected.
What am I missing?
Upvotes: 2
Views: 884
Reputation: 1001
Please modify your jQuery code and replace it with below code:
jQuery(document.body).on('change', 'select[name=billing_country]', function(){
setTimeout(function() {jQuery(document.body).trigger('wc_fragment_refresh');},1000);
});
Note: Also, another solution is to use Ajax feature and run the "wc_frament_refresh" on ajax success.
Upvotes: 4