简体   繁体   中英

Update cart shipping data with AJAX in WooCommerce

I'm trying to update my checkout cart's shipping with AJAX...

I've got the action in functions.php

function jwd_update_shipping()
{
    WC()->cart->calculate_shipping();

    echo "hi";
    die();
}
add_action('jwd_update_shipping_callback', 'jwd_update_shipping');

Then in js I call it, like so

jQuery.ajax({
    type: "POST",
    url: 'MYSITE.com/wp-admin/admin-ajax.php',
    data: ({
        action: "jwd_update_shipping"
    }),
    success: function(response) {
        console.log("got this: " + response);
        if (response.type == "success") {
            alert("here");
            jQuery('body').trigger('update_checkout');
        } else {
            alert("fail");
        }
    },
    error: function(request, status, error) {
        alert(request.responseText);
    }
});

I just get a 0 alerted back at me, which suggests that the AJAX call failed.

First your ajax request is not reaching the related php function… Also you need more things to refresh checkout… Try the following instead:

// The jQuery script that send the Ajax request
add_action( 'wp_footer', 'refresh_shipping_js' );
function refresh_shipping_js() {
    // Only on checkout
    if( is_checkout() && ! is_wc_endpoint_url() ):
    ?>
    <script type="text/javascript">
    jQuery( function($){
        if (typeof wc_checkout_params === 'undefined') 
            return false;

        var refresh = 'yes';

        $.ajax({
            type: "POST",
            url: wc_checkout_params.ajax_url,
            data: ({
                'action': 'updating_shipping',
                'refresh_shipping': refresh,

            }),
            success: function(response) {
                if( response === '1' ) {
                    $(document.body).trigger('update_checkout');
                    console.log('Success: '+response); // For testing (to be removed)
                } else {
                    console.log('Failled: '+response); // For testing (to be removed)
                }
            },
            error:function(error) {
                console.log('Error: '+error); // For testing (to be removed)
            }
        });
    });
    </script>
    <?php
    endif;
}

// function that gets the Ajax data
add_action( 'wp_ajax_updating_shipping', 'updating_shipping' );
add_action( 'wp_ajax_nopriv_updating_shipping', 'updating_shipping' );
function updating_shipping() {
    if ( isset($_POST['refresh_shipping']) && $_POST['refresh_shipping'] === 'yes' ){
        WC()->session->set('refresh_shipping', '1' );
    } else {
        WC()->session->set('refresh_shipping', '0' );
    }
    echo  WC()->session->get('refresh_shipping');
    die(); // Alway at the end (to avoid server error 500)
}

// Function that refresh session shipping methods data
add_action( 'woocommerce_checkout_update_order_review', 'refresh_shipping_methods', 10, 1 );
function refresh_shipping_methods( $post_data ){
    if ( WC()->session->get('refresh_shipping' ) === '1' ) {
        foreach ( WC()->cart->get_shipping_packages() as $package_key => $package ){
            WC()->session->set( 'shipping_for_package_' . $package_key, false );
        }
        WC()->cart->calculate_shipping();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM