Michael Fitzgerald
Michael Fitzgerald

Reputation: 37

Executing Query String in Wordpress without refresh

I have found the following code to empty my cart on wordpress/woocommerce.

add_action('init', 'woocommerce_clear_cart_url');
function woocommerce_clear_cart_url() {
    global $woocommerce;
    if( isset($_REQUEST['clear-cart']) ) {
        $woocommerce->cart->empty_cart();
    }
}

To execute this I add ?clear-cart to any URL on my site, I'm a little over my head here.

But I was wondering if there was a way using jQuery or something to execute this function without a page refresh?

Upvotes: 0

Views: 570

Answers (1)

Ygg
Ygg

Reputation: 3870

Assuming the URL for clearing your cart is host/cart/clear

You can access that URL by sending an AJAX POST request to the server:

var request = $.post('host/cart/clear');

If you want to attach event handlers for the sucess and/or failure of the request:

request.done(successHandler);
request.fail(failureHandler);

Where success- and failureHandler are functions.

Upvotes: 1

Related Questions