update cart shipping woocommerce when change country with ajax

人走茶凉 提交于 2021-01-27 11:26:14

问题


i have 3 zone shipping and every zone have 3 more method shipping (diffrent)

  1. Zone US : Method Shipping = 1. Flate_rate1, 2. Flate_rate2, 3. Flate_rate3

  2. Zone UK : Method Shipping = 1. Flate_rate4, 2. Flate_rate5, 3. Flate_rate6

  3. Zone All WOrld : Method Shipping = 1. Flate_rate7, 2. Flate_rate8

now iwant to display method shipping by zone when select billing country with ajax.

example : if select billing country US then only display/showing method shipping by zone us (1. Flate_rate1, 2. Flate_rate2, 3. Flate_rate3)

how do that with ajax, but no refresh page.

any can help me... thank's.. if any help you can send by email : andythreesaputra@gmail.com or answer this forum. thank's very much


回答1:


You can achieve this using the WooCommerce filter 'woocommerce_package_rates'.

You can code this way

add_filter('woocommerce_package_rates', 'wf_modify_rates', 10, 3);
function wf_modify_rates($available_shipping_methods, $package){
    $methords_us = array('flaterate:1','flaterate:2');

    if( $package['destination']['country'] == 'US'){
        foreach ($available_shipping_methods as $methord_name => $methord) {
            if(!in_array($methord_name, $methords_us)){
                unset($available_shipping_methods[$methord_name]);
            }
        }
    }
    return $available_shipping_methods;
}


来源:https://stackoverflow.com/questions/39424014/update-cart-shipping-woocommerce-when-change-country-with-ajax

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