how to make remove product from cart ajax ?

Home » Snippets » how to make remove product from cart ajax ?

Created with:



Creator: majed

Customize with WPTurbo AI

Add Snippet To Project

New Project
Add To Existing Project
add_action('wp_ajax_remove_product_from_cart', 'wpturbo_remove_product_from_cart');
add_action('wp_ajax_nopriv_remove_product_from_cart', 'wpturbo_remove_product_from_cart');

function wpturbo_remove_product_from_cart() {
    // Check if the request is valid
    if (isset($_POST['product_id'])) {
        $product_id = sanitize_text_field($_POST['product_id']);
        // Remove the product from the cart
        // Return a success response
    } else {
        // Return an error response
        wp_send_json_error('Invalid request');

Make sure to replace $product_id with the actual product ID in your template.

With these changes, when the user clicks on the "Remove" button, an Ajax request will be sent to the server, and the product will be removed from the cart without refreshing the page. The cart page or cart display will be updated automatically.

Register an account to save your snippets or go Pro to get more features.