javascript update_order_review( ) 按钮点击
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27635186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
update_order_review( ) on button click
提问by user1049961
I have a custom button on my checkout page, on click I'm adding a product to cart via AJAX.
我的结帐页面上有一个自定义按钮,单击后我将通过 AJAX 将产品添加到购物车。
JS:
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:
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?
之后,我需要刷新订单审核,以便它也显示我新添加的产品。我怎样才能做到这一点?
回答by Rick Dupont
All you need to do is call a trigger on the body to update the cart.
您需要做的就是调用 body 上的触发器来更新购物车。
$( 'body' ).trigger( 'update_checkout' );
This will automatically call all the subsequent AJAX calls needed to refresh the cart information, including the order review.
这将自动调用刷新购物车信息所需的所有后续 AJAX 调用,包括订单审核。
回答by Hamid Mohayeji
In Checkout page:
在结帐页面:
jQuery(document.body).trigger("update_checkout")
In Cart page:
在购物车页面:
jQuery(document.body).trigger("wc_update_cart");