Support » Fixing WordPress » Update shipping cost with ajax

  • Hi,
    In checkout page I have google address autocomplete and embeded map with polygons drawn as delivery zones. When user enters address, js function is triggered to check if entered address is inside any of the polygons and it returns zone number (0-4) via ajax request.
    After that I would like to change shipping cost based on zone number and total order cost. I have tried many different ways to make it work but none have worked so far…

    Here’s my ajax function:

                           var data = {
                                'action': 'delivery_zones',
                                'zone': deliveryZone // int 0-4
                            };
    
                            jQuery.ajax({
                                url: ajax_object.ajaxurl,
                                type: 'POST',
                                data: data,
                                dataType: 'json',
                                success: function(response) {
                                    console.log(response);
                                },
                                complete: function() {
                                    console.log('completed');
                                }
                            });
    

    Here’s my php 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 = $_POST['zone'];
    	echo json_encode($zone);
     	wp_die();
    } 

    Im getting my ajax response just fine, however, now I need to change shipping cost which I dont know how to do. Hoping someone knows a solution.

    • This topic was modified 3 years, 10 months ago by Jan Dembowski. Reason: Moved to Fixing WordPress, this is not an Developing with WordPress topic
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Update shipping cost with ajax’ is closed to new replies.