Our site has a lengthy AJAX function that appends content to the user's cart, which is stored as an array in the usermeta
table.
The function looks something like this:
$cart = get_user_meta($userID, 'cart', true);
$cart[] = $newItemID;
update_user_meta($userID, 'cart', $cart);
We're running into a problem where, when the user tries to add multiple items to their cart in rapid succession, the final call to update_user_meta will sometimes overwrite the cart with old data.
From what I understand, this is a race condition which is partly caused by the fact that WordPress stores array data as a serialized string rather than as a set of table rows.
I know that I could just create a cart
table and use queries to manage it, but I was hoping that there was a way to do it with the existing get_user_meta/update_user_meta functions.