WooCommerce: Update User Meta After a Successful Order

 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 ðŸ™‚

WooCommerce: edit user meta after successful checkout

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 CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 4.6
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */
 
add_action( 'woocommerce_thankyou', 'bbloomer_checkout_save_user_meta' );
 
function bbloomer_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() );
   }
}

No comments:

Post a Comment

Free Shipping Threshold @ Cart Page