Shipping calculator state field change trigger update in WooCommerce cart [closed]

独自空忆成欢 提交于 2020-08-10 19:18:15

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!