guys! I faced some issue again. For a project on WooCommerce I created the custom system favorite products. The default one did not suit us.I could implement the addition created this code:
Button for adding to the favorite list
<span data-post-id="<?php the_ID(); ?>" data-user-id="<?php echo $current_user->ID; ?>" class="product__favorite product_favor"></span>
The jQuery code
jQuery('.product_favor').on('click',function(e){
var postID = jQuery(this).attr("data-post-id");
var userID = jQuery(this).attr("data-user-id");
data = {
'action': 'dicaprio_add_fav_product',
'postID': postID,
'userID': userID
}
jQuery.ajax({
type:'post',
url: object_url.url,
data: data,
cache: false,
success: function(data) {
}
});
e.preventDefault();
});
The code in function:
add_action( 'wp_ajax_nopriv_dicaprio_add_fav_product', 'dicaprio_add_fav_product' );
add_action( 'wp_ajax_dicaprio_add_fav_product', 'dicaprio_add_fav_product' );
function dicaprio_add_fav_product() {
$postID = $_POST['postID'];
$userID = $_POST['userID'];
$current_fav_list = get_user_meta( $userID, 'favorite_list_product', true );
if ( is_array( $current_fav_list ) ) {
if ( in_array( $postID, $current_fav_list ) ) {
} else {
$current_fav_list[] = $postID;
}
} else {
$current_fav_list = array();
$current_fav_list[] = $postID;
}
update_usermeta( $userID, 'favorite_list_product', $current_fav_list );
}
But I have not thought about the removal system. No, I thought that I could do everything the other way around. But it doesn’t work. This is how I decided to organize the removal of an item from favorites.
Delete button
<?php if(is_page_template('page-favorite.php') ): ?>
<div style="display: none" class="remove_product" data-post-id="<?php the_ID(); ?>" data-user-id="<?php echo $current_user->ID; ?>" class="remove_from_favorite">Remove</div>
<?php endif; ?>
The jQuery code for remove
jQuery('.remove_product').on('click',function(e){
var postID = jQuery(this).attr("data-post-id");
var userID = jQuery(this).attr("data-user-id");
alert(postID)
data = {
'action': 'dicaprio_remove_fav_prod',
'postID': postID,
'userID': userID
}
jQuery.ajax({
type:'post',
url: object_url.url,
data: data,
cache: false,
success: function(data) {
}
});
e.preventDefault();
});
And the function code to remove
add_action( 'wp_ajax_nopriv_dicaprio_add_fav_prod', 'dicaprio_remove_fav_prod' );
add_action( 'wp_ajax_dicaprio_add_fav_prod', 'dicaprio_remove_fav_prod' );
function dicaprio_remove_fav_prod() {
$postID = $_POST['postID'];
$userID = $_POST['userID'];
$current_fav_list = get_user_meta( $userID, 'favorite_list_product', true );
if ( is_array( $current_fav_list ) ) {
if ( in_array( $postID, $current_fav_list ) ) {
} else {
$current_fav_list[] = $postID;
}
} else {
$current_fav_list = array();
$current_fav_list[] = $postID;
}
update_usermeta( $userID, 'favorite_list_product', $current_fav_list );
}
But deleting doesn’t work. When I click on the delete button, Ajax return 400 error. Here is screenshot:
I tried but couldn’t figure out what was going on. And I suspect that the removal code has errors, but I just don’t see them. Please help me solve this issue.
Advertisement
Answer
Change your code for remove from favorites:
add_action( 'wp_ajax_nopriv_dicaprio_add_fav_prod', 'dicaprio_remove_fav_prod' );
add_action( 'wp_ajax_dicaprio_add_fav_prod', 'dicaprio_remove_fav_prod' );
To this:
add_action( 'wp_ajax_nopriv_dicaprio_remove_fav_prod', 'dicaprio_remove_fav_prod' );
add_action( 'wp_ajax_dicaprio_remove_fav_prod', 'dicaprio_remove_fav_prod' );
You use same actions to add and remove items.
And one more: when you use somethink from user input ($_POST, $_GET, etc) – you must to sanitize this data before use.