【发布时间】:2018-11-01 18:11:04
【问题描述】:
我正在构建一个批量订单表单插件(用于 wordpress/woocommerce)——所有添加到购物车的功能都运行良好。我正在努力创建一个“取消订单”按钮,按下该按钮会清除所有项目行(此位有效)并从购物车中删除所有项目。
我正在尝试使用 AJAX/js、php 和标准 HTML 的组合..:
我的按钮..:
<button class="btn btn-danger btn-lg" id="cancelorder">Cancel Order</button>
我的购物车清空功能..:
add_action( 'init', 'woocommerce_clear_cart_url' );
function woocommerce_clear_cart_url() {
global $woocommerce;
if ( isset( $_GET['empty-cart'] ) ) {
$woocommerce->cart->empty_cart();
}
}
最后,我的 js 函数/ajax 调用..:
$("#cancelorder").click(function(){
if(confirm('Are you sure you want to clear all rows?')){
$(".addedrow").remove(); //removes line items - not related to issue
$.ajax({
type: "POST",
url: '/wp-admin/admin-ajax.php?action=woocommerce_clear_cart_url',
data: {action : 'woocommerce_clear_cart_url'},
success: function (res) {
if (res) {
alert('Removed Successfully');
}
}
});
} else {
//back out with no action
}
});
行已从表单中删除,但商品仍保留在购物车中。
【问题讨论】:
标签: javascript php ajax wordpress woocommerce