admin管理员组

文章数量:1185453

I'm trying to create a simple "favorite" product list. Basically, when viewing a list of products someone can click a button that adds it to your favorites.

The idea is, they click the button and ajax adds the productID to an array in the user meta. The next time they click the same button is should remove the productID from the user meta.

I have all the ajax setup and the first time the button is clicked it adds the productID. However, if I click it again it gives me an error instead of removing the ID from the user meta. The error is just "Error updating user meta."

I believe the error is coming from unset($fav_products[$product_id]);

Here is the PHP function being called by ajax

function update_user_meta_ajax() {
    $user_id = isset( $_POST['user_id'] ) ? intval( $_POST['user_id'] ) : 0;
    $product_id = isset( $_POST['product_id'] ) ? sanitize_text_field( $_POST['product_id'] ) : '';
    $meta_key = isset( $_POST['meta_key'] ) ? sanitize_text_field( $_POST['meta_key'] ) : '';
    $meta_value = isset( $_POST['meta_value'] ) ? sanitize_text_field( $_POST['meta_value'] ) : '';

    // Get Saved Array
    $fav_products = get_user_meta($user_id, 'fav_products', true);
    
    // Check if array is empty
    if (empty($fav_products)) {
        $fav_products = array(); 
    } 

    // Add or Remove Item from Array
    if($meta_value == 'yes'){
        $fav_products[] = $product_id; 
    }else{
        unset($fav_products[$product_id]);
    }

    if (! current_user_can('edit_user', $user_id)) {
        wp_send_json_error(array('message' => 'Insufficient permissions.'));
    }

    if (update_user_meta($user_id, $meta_key, $fav_products)) {
        wp_send_json_success();
    } else {
        wp_send_json_error(array('message' => 'Failed to update user meta.'));
    }

    wp_die();
}

I'm trying to create a simple "favorite" product list. Basically, when viewing a list of products someone can click a button that adds it to your favorites.

The idea is, they click the button and ajax adds the productID to an array in the user meta. The next time they click the same button is should remove the productID from the user meta.

I have all the ajax setup and the first time the button is clicked it adds the productID. However, if I click it again it gives me an error instead of removing the ID from the user meta. The error is just "Error updating user meta."

I believe the error is coming from unset($fav_products[$product_id]);

Here is the PHP function being called by ajax

function update_user_meta_ajax() {
    $user_id = isset( $_POST['user_id'] ) ? intval( $_POST['user_id'] ) : 0;
    $product_id = isset( $_POST['product_id'] ) ? sanitize_text_field( $_POST['product_id'] ) : '';
    $meta_key = isset( $_POST['meta_key'] ) ? sanitize_text_field( $_POST['meta_key'] ) : '';
    $meta_value = isset( $_POST['meta_value'] ) ? sanitize_text_field( $_POST['meta_value'] ) : '';

    // Get Saved Array
    $fav_products = get_user_meta($user_id, 'fav_products', true);
    
    // Check if array is empty
    if (empty($fav_products)) {
        $fav_products = array(); 
    } 

    // Add or Remove Item from Array
    if($meta_value == 'yes'){
        $fav_products[] = $product_id; 
    }else{
        unset($fav_products[$product_id]);
    }

    if (! current_user_can('edit_user', $user_id)) {
        wp_send_json_error(array('message' => 'Insufficient permissions.'));
    }

    if (update_user_meta($user_id, $meta_key, $fav_products)) {
        wp_send_json_success();
    } else {
        wp_send_json_error(array('message' => 'Failed to update user meta.'));
    }

    wp_die();
}
Share Improve this question asked Jan 27 at 2:03 JonJon 3412 silver badges13 bronze badges 2
  • Are you struggling with the JS part or with the PHP part? What did you try to resolve the problem? "Error updating user meta" isn't an error message thrown by the code you've shared so far – Nico Haase Commented Jan 27 at 9:13
  • No, I mentioned in the question where I thought the problem is. I've already answered the question anyway, I just can't accept it for a couple days. – Jon Commented Jan 27 at 14:22
Add a comment  | 

1 Answer 1

Reset to default 0

I figured it out. The problem was I was trying to delete an item in the array with a value and not a key.

This fixed it for me, I replaced the else code with the following...

if (($key = array_search($product_id, $fav_products)) !== false) {
   unset($fav_products[$key]);
}

本文标签: phpWordPress AddRemove User Meta via AJAXStack Overflow