64
When an order is placed in WooCommerce, you might want to change/add something in the User Meta programmatically.
For example, you could “check” a custom checkbox in the User Profile. Or maybe assign the User Twitter username. And so on 🙂
PHP Snippet: Update User Meta After a Successful Order in WooCommerce
In this example, we’re saving the customer IP address into a user custom field. You can get other order data by checking https://www.businessbloomer.com/woocommerce-easily-get-order-info-total-items-etc-from-order-object/.
/**
* @snippet Update User Meta After a Successful Order - WooCommerce
* @how-to Get tutoraspire.com FREE
* @author Tutor Aspire
* @compatible WooCommerce 4.6
* @donate $9 https://tutoraspire.com
*/
add_action( 'woocommerce_thankyou', 'tutoraspire_checkout_save_user_meta' );
function tutoraspire_checkout_save_user_meta( $order_id ) {
$order = wc_get_order( $order_id );
$user_id = $order->get_user_id();
if ( $order->get_customer_ip_address() ) {
update_user_meta( $user_id, 'latest_ip_address', $order->get_customer_ip_address() );
}
}