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_cart_item', 'wpturbo_remove_cart_item');
add_action('wp_ajax_nopriv_remove_cart_item', 'wpturbo_remove_cart_item');

function wpturbo_remove_cart_item() {
    // Verify the nonce
    check_ajax_referer('remove_cart_item_nonce', 'nonce');
    // Check if the request is valid
    if (isset($_POST['cart_item_key'])) {
        $cart_item_key = sanitize_text_field($_POST['cart_item_key']);
        // Remove the cart item
        // Return a success response
    } else {
        // Return an error response
        wp_send_json_error('Invalid request');

Make sure to add a nonce field to your Ajax request data. For example:

Remember to enqueue the nonce value in your script using wp_localize_script(). Replace wpturbo_ajax_object.nonce with the appropriate variable.

By implementing these additional security measures, you can further protect your code against potential vulnerabilities.

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