0
X
Add Snippet To Project
New Project
Add To Existing Project
<?php
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
WC()->cart->remove_cart_item($cart_item_key);
// Return a success response
wp_send_json_success();
} 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.