I am trying to update user meta fields after a Successful Order in WooCommerce
This is my shortcode:
add_action( 'woocommerce_thankyou', 'check_order_product_id', 10, 1);
function check_order_product_id( $order_id ){
# Get an instance of WC_Order object
$order = wc_get_order( $order_id );
$user_id = $order->get_user_id();
# Iterating through each order items (WC_Order_Item_Product objects in WC 3+)
foreach ( $order->get_items() as $item_id => $item_values ) {
// Product_id
$product_id = $item_values->get_product_id();
// OR the Product id from the item data
$item_data = $item_values->get_data();
$product_id = $item_data['product_id'];
$nextyear = date('d/m/Y', strtotime('+1 year'));
# Targeting a defined product ID
if ( $product_id == 24419 ) {
add_user_meta( $user_id, 'wcpf-vip-exp', $nextyear);
}
}
}
add_action( 'woocommerce_thankyou', 'your_function_name', 10);
But when I completed the order
User meta still blank
Hi, what kind of field is this vip-exp? If it's a date field that was created in Types, then you must store a Unix timestamp format:
$nextyear = date('U', strtotime('+1 year'));
Also, you should probably call "update_user_meta" instead of "add_user_meta", unless you are 100% sure the vip-exp value will not exist yet.
https://codex.wordpress.org/Function_Reference/update_user_meta
If those two changes do not fix the problem, I will be glad to take a closer look.
Hello,
It's dont fix the problem.
The vip-exp value still blank
Hello
I try to update another fields,
update_user_meta( $user_id , 'wcpf-vip', $nextyear); #not-working
update_user_meta( $user_id , 'first_name', $nextyear ); #working
update_user_meta( $user_id , 'last_name', $nextyear ); #working
Hi there,
Thank you for waiting and I'll be happy to follow up on this thread, while Christian is on holidays.
I noticed in your messages that the prefix used before the user field slug is "wcpf-", whereas it should be "wpcf-".
Please make sure to use the correct prefix and the user field slug and then test the function again.
regards,
Waqar
My issue is resolved now. Thank you!