I have google autocomplete api on my address field in checkout page and embeded map with polygons as delivery zones. Each zone has it's own delivery cost which also depends on order's total cost. When address is entered, js function is triggered to check if address is inside of any of the polygons and it makes ajax call to functions.php passing variable 'zone' (int value from 0 to 4).

As of right now, I can pass variable to function, update checkout and get success callback, but I am having difficulty to understand what's the best way to change shipping cost for flat_rate method when this function is triggered.

Here's my js:

                        var data = {
                            'action': 'delivery_zones',
                            'zone': deliveryZone // int 0-4

                            url: ajax_object.ajaxurl,
                            type: 'POST',
                            data: data,
                            success: function(response) {
                            complete: function() {

Here's my function:

function so_enqueue_scripts(){
  wp_register_script( 'ajaxHandle', get_template_directory_uri() . '/js/delivery_zones.js', array(), false, true );
  wp_enqueue_script( 'ajaxHandle' );
  wp_localize_script( 'ajaxHandle', 'ajax_object', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
add_action( 'wp_enqueue_scripts', 'so_enqueue_scripts' );

add_action( 'wp_ajax_delivery_zones', 'delivery_zones' );
add_action( 'wp_ajax_nopriv_delivery_zones', 'delivery_zones' );

function delivery_zones() {
    $zone = '';
    if(isset($_POST['zone'])) {
        $zone = $_POST['zone'];

    echo $zone;

Here's my ajax success callback example if zone is set to 2:


Is there some way I can call function with woocommerce_package_rates filter and 'zone' as one of arguments after checkout is updated?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.