简体   繁体   English

使用AJAX从自定义页面上的按钮中删除所有购物车项目

[英]Remove all cart items from a button on a custom page using AJAX

I'm building a bulk order form plugin (for use with wordpress/woocommerce) - all add to cart functionality is working fine. 我正在构建一个批量订单表单插件(用于wordpress / woocommerce)-所有添加到购物车的功能都运行良好。 Where I am struggling is in creating a "Cancel Order" button which, when pressed, clears all item rows (this bit works) as well as removes all items from the cart. 我正在努力创建“取消订单”按钮,按下该按钮可以清除所有项目行(此位有效)以及从购物车中删除所有项目。

I am attempting this using a combination of AJAX/js, php and standard HTML..: 我正在尝试结合使用AJAX / js,php和标准HTML ..:

My button..: 我的按钮..:

<button class="btn btn-danger btn-lg" id="cancelorder">Cancel Order</button>

My cart-empty function..: 我的购物车清空功能..:

add_action( 'init', 'woocommerce_clear_cart_url' );

function woocommerce_clear_cart_url() {
    global $woocommerce;
    if ( isset( $_GET['empty-cart'] ) ) {
        $woocommerce->cart->empty_cart();
    }
}

and finally, my js function/ajax call..: 最后,我的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
    }
});

Rows are removed from the form, but the items remain in the cart. 行从窗体中删除,但项目保留在购物车中。

Well, looks like you are sending a POST request 好吧,看来您正在发送POST请求

type: "POST"

and trying to recover a GET parameter on your controller 并尝试恢复控制器上的GET参数

if ( isset( $_GET['empty-cart'] ) )

Plus, the key you're looking for ( 'empty-cart' ) doesn't even seem to exist... At least in this portion of code you provided... 另外,您正在寻找的键( 'empty-cart' )似乎根本不存在...至少在您提供的这段代码中...

Update: I was able to get this working by modifying the existing code above to the following..: 更新:通过将上面的现有代码修改为以下内容,我能够使它正常工作:

Cart-Empty function..: 空车功能..:

add_action('wp_ajax_wc_woocommerce_clear_cart_url', 'wc_woocommerce_clear_cart_url');
add_action('wp_ajax_nopriv_wc_woocommerce_clear_cart_url', 'wc_woocommerce_clear_cart_url'); 
//added wc_ prefix in case of function name conflict

function wc_woocommerce_clear_cart_url() {
global $woocommerce;
$returned = ['status'=>'error','msg'=>'Your order could not be emptied'];
$woocommerce->cart->empty_cart();
if ( $woocommerce->cart->get_cart_contents_count() == 0 ) {    
    $returned = ['status'=>'success','msg'=>'Your order has been reset!'];       
}
die(json_encode($returned));
}

and the js/ajax side..: 和js / ajax方面..:

$("#cancelorder").on('click',function(){        
    if(confirm('Are you sure you want to clear all rows?')){
        $.ajax({
        type: 'POST',
        dataType: 'json',
        url: '/wp-admin/admin-ajax.php?action=wc_woocommerce_clear_cart_url',
        data: {action : 'wc_woocommerce_clear_cart_url'},
        success: function (data) {
                if (data.status != 'success') {
                    alert(data.msg);
                } else {
                    $('#itemrows').html('');
                    addrows();
                }
            }   
        });
    } else {
        //back out with no action
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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