问题
In the code below, I am trying to auto update the shipping calculator in WooCommerce cart page cart page when the selected state field is changed, to display shipping cost by state region:
add_action('wp_footer', 'state_update_checkout', 50);
function state_update_checkout() {
if ( ! is_cart() ) return;
?>
<script type="text/javascript">
$("[name='calc_shipping_state']").on('change', function(e) {
$("[name='calc_shopping']").trigger("click");
</script>
<?php
}
But it doesn't work. What I am doing wrong?
回答1:
2020 Update: There are some mistakes in your code… Try the following instead:
add_action('wp_footer', 'state_update_checkout', 50);
function state_update_checkout() {
if ( ! is_cart() ) return;
?>
<script type='text/javascript'>
jQuery(function($){
$(document.body).on('change', 'select[name="calc_shipping_state"]', function() {
$(this).submit();
});
});
</script>
<?php
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
Related: Update Woocommerce cart shipping methods on country change
来源:https://stackoverflow.com/questions/62006414/shipping-calculator-state-field-change-trigger-update-in-woocommerce-cart