Skip Navigation

[Resolved] Update user meta fields after a Successful Order in WooCommerce

This support ticket is created 5 years, 10 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 5 replies, has 3 voices.

Last updated by eloiD 5 years, 10 months ago.

Assisted by: Waqar.

Author
Posts
#1171730

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

#1172075

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.

#1172175

Hello,
It's dont fix the problem.

The vip-exp value still blank

#1172258

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
#1173363

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

#1176740

My issue is resolved now. Thank you!