Woocommerce Version 10.1.2 delete meta tags for our own good
-
We have a WP site where some User content is updated from an external data source.
- It is not a CRON job that connects to a remote data source, queries for data, or calls an API to digest data, and then logs into WP to use WP functions to update/insert User meta data.
- The external data source connects directly with the WP database and updates User data and User meta data.
A few months into our process, we ran into an issue with disappearing User meta tags. Some of the User meta tags that were added for Users were disappearing apparently for random Users. They were disappearing because Woocommerce was deleting them. Again not for all Users, just some, as well as not all custom Meta tags, just some.
It may have started with Woocommerce Version 10.1.2, or perhaps the version before it.
WooCommerce gets all current meta for the customer, compares it to what’s in its memory (
WC_Customerobject), and deletes any meta not present in the in-memory object.In other words, the Woocommerce plugin, at checkout, when it executes WC_Customer->save(), will delete any User Meta tags it does not think should belong to the User.
That is an inconvenient non-documented feature if you know what I mean.
/wp-content/plugins/woocommerce/includes/data-stores/class-wc-data-store-wp.php:132
→ delete_metadata_by_mid()The function delete_metadata_by_mid() is deleting meta rows by meta ID, not by meta key, and therefore bypasses ALL standard WP protections. (“is_protected_meta()”, “register_protected_user_meta()”, “pre_delete_user_meta”, etc.)
So, if you have any User meta tags that randomly ‘disappear’ and you have the Woocommerce plugin, put in a hook with a backtrace and you might be able to determine if it is Woocommerce doing it:
function log_user_meta_deletion($meta_ids, $user_id, $meta_key, $meta_value) {
$time = current_time('mysql');
$log_message = sprintf(
"[%s] User ID: %d - Meta Key DELETED: %s - Deleted Value: %s\n",
$time,
$user_id,
$meta_key,
maybe_serialize($meta_value)
);
$log_file = WP_CONTENT_DIR . '/uploads/user-meta-updates.log';
error_log($log_message, 3, $log_file);
// stack trace when custom meta tags are being deleted
foreach ($meta_ids as $meta_id) {
$meta = get_metadata_by_mid( 'user', $meta_id );
if (in_array($meta->meta_key, ['MyMissingMetaKey'])) {
$log_file = WP_CONTENT_DIR . '/uploads/user-meta-updates.log';
error_log("Deleted meta key: {$meta->meta_key} for user $user_id", 3, $log_file);
error_log("Backtrace:\n" . print_r(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), true), 3, $log_file);
}
}
}The only way that I found to make sure Woocommerce does not delete meta data records from wp_usermeta that are not added using standard WP methods, is to make force them to be added to the Woocommerce customer object.
// add custom meta keys to woocommerce
add_action( 'woocommerce_before_checkout_process', function() {
if ( ! is_user_logged_in() ) {
return;
}
$user_id = get_current_user_id();
$customer = new WC_Customer( $user_id );
$custom_keys = [
'MyMissingMetaKey1',
'MyMissingMetaKey2',
'MyMissingMetaKey3',
'MyMissingMetaKey4',
];
foreach ( $custom_keys as $key ) {
$value = get_user_meta( $user_id, $key, true );
$customer->update_meta_data( $key, $value );
}
// Save manually so WooCommerce doesn't delete meta when updating WC Customer Object
$customer->save();
}, 5 );At the time of posting this, we are no longer losing user meta data.
-
Hi @curtisfraser,
Thank you for reaching out and for sharing the detailed workaround that worked for you. I’m glad you were able to create a code solution that resolved the issue in your case. I’m sure anyone with a similar setup or facing the same problem will find this very helpful.
At this time, the issue involves a custom customization, which is beyond the scope of support we can provide. However, if you’d like the feature update to be officially reverted or a formal workaround implemented, you can submit an issue here: https://github.com/woocommerce/woocommerce/issues.
Thank you once again for sharing this. It is much appreciated.
Thanks for the reply Moses. We have not customized anything with WooCommerce plugin, nor with WP core.
I will use the link you provided. I would like to find out if there is a specific reason that WooCommerce is using delete_metadata_by_mid() to remove meta tags that are not in its customer object. This seems to be somewhat out of scope for what the WooCommerce plugin is used for. I would think that the WordPress developers would agree.
A plugin can use get_user_meta to retrieve meta keys, which are within the MySQL table wp_usermeta associated with a User.
HI @curtisfraser,
Thank you for your response, and I completely understand your concern. Please create an issue using the link I shared earlier so the development team can review it and provide a direct explanation.
In case you want to follow the thread:
Hi there!
Thank you for sharing the GitHub thread URL — this will be helpful for others who may face a similar problem. I can also see that our developers are already assisting you directly in that thread, and hopefully they will help you get this issue fixed.Hi there!
Thank you for sharing the video. From the video, it looks like you’re using the Add to Cart + Options (Beta) block, and I can see that you’ve selected the Simple Product option in this block.
Could you please click on the eye icon, switch to the Variable Product option, and then test the issue again? You can see how to do this in the following screenshot:
👉 https://go.screenpal.com/watch/cTQXienoz6nLet us know if that works for you.
The topic ‘Woocommerce Version 10.1.2 delete meta tags for our own good’ is closed to new replies.