Skip to content
Advertisement

update_order_review( ) on button click

I have a custom button on my checkout page, on click I’m adding a product to cart via AJAX.

JS:

$('#add_domain_product').on('click', function() {
        $.ajax({
            url: Ajax.ajaxurl,
            type: "POST",
            data: {
                action: 'add_domain_product',
            },
            success: function (data, status, xhr) {
                // update command is executed.
                console.log(data);
            }

        });
    })

PHP:

add_action('wp_ajax_add_domain_product', 'bs_add_domain_product');
function bs_add_domain_product() {
    global $woocommerce;
    $woocommerce->cart->add_to_cart('633');
    exit();
}

After that, I’d need to refresh the order review, so it displays my newly added product also. How can I do that?

Advertisement

Answer

All you need to do is call a trigger on the body to update the cart.

$( 'body' ).trigger( 'update_checkout' );

This will automatically call all the subsequent AJAX calls needed to refresh the cart information, including the order review.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement