Reputation: 2736
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?
Upvotes: 10
Views: 22744
Reputation: 4275
In Checkout page:
jQuery(document.body).trigger("update_checkout")
In Cart page:
jQuery(document.body).trigger("wc_update_cart");
Upvotes: 11
Reputation: 356
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.
Upvotes: 34