简体   繁体   中英

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. 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..:

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..:

$("#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

type: "POST"

and trying to recover a GET parameter on your controller

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...

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..:

$("#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
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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