• Resolved fabio323ti

    (@fabio323ti)


    someone can help me to update user meta field after checkout complete?

    i already have snippet to sync userprofile meta data with wocoommerce billing field

    add_filter( 'profile_update' , 'custom_update_checkout_fields', 10, 2 );
    function custom_update_checkout_fields($user_id, $old_user_data ) {
      $current_user = wp_get_current_user();
    
      // Updating Billing info
      if($current_user->user_firstname != $current_user->billing_first_name)
        update_user_meta($user_id, 'billing_first_name', $current_user->user_firstname);
      if($current_user->user_lastname != $current_user->billing_last_name)
        update_user_meta($user_id, 'billing_last_name', $current_user->user_lastname);
      if($current_user->user_email != $current_user->billing_email)
        update_user_meta($user_id, 'billing_email', $current_user->user_email);
      if($current_user->company != $current_user->billing_company)
        update_user_meta($user_id, 'billing_company', $current_user->company);
      if($current_user->telefono != $current_user->billing_phone)
        update_user_meta($user_id, 'billing_phone', $current_user->telefono);
      if($current_user->vat != $current_user->billing_piva)
        update_user_meta($user_id, 'billing_piva', $current_user->vat);	
       if($current_user->sdi != $current_user->billing_code_sdi)
        update_user_meta($user_id, 'billing_code_sdi', $current_user->sdi);	
    	
    	
    }

    wish to do the same if customer change some chckout field… without success

    im using this snipped but not works

    add_action( 'woocommerce_thankyou', 'update_usermeta_from_billing_checkout' );
     
    function update_usermeta_from_billing_checkout( $user_id ) {
       	$order = wc_get_order( $order_id );
        
        update_user_meta( $user_id, 'first_name', $order->get_billing_first_name() ); 
        update_user_meta( $user_id, 'last_name', $order->get_billing_last_name() );
    	update_user_meta( $user_id, 'vat', $order->get_billing_piva() ); 
    	update_user_meta( $user_id, 'sdi', $order->get_billing_code_sdi() ); 
    	update_user_meta( $user_id, 'telefono', $order->get_billing_phone() ); 
      
    }

    someone please can help?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Mirko P.

    (@rainfallnixfig)

    Hi there,

    This is a fairly complex development topic. I’m going to leave it open for a bit to see if anyone is able to chime in to help you out.

    I can also recommend the WooCommerce Developer Resources Portal for resources on developing for WooCommerce.

    You can also visit the WooCommerce Facebook Community group or the #developers channel of the WooCommerce Community Slack. We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there, as well.

    Cheers.

    Thread Starter fabio323ti

    (@fabio323ti)

    Mirko, i’d solved myself, thanks for support.
    im share the snippet if someone need it.

    add_action( 'woocommerce_checkout_order_processed', 'update_usermeta_from_billing_checkout' );
     
    function update_usermeta_from_billing_checkout( $order_id ) {
       	$order = wc_get_order( $order_id );
    	$order_data = $order->get_data();
     	$user_id = $order->get_user_id();
    
    	$order_billing_firstname = $order_data['billing']['first_name'];
    	$order_billing_lastname = $order_data['billing']['last_name'];
    	$order_billing_company = $order_data['billing']['company'];
    	$order_billing_phone = $order_data['billing']['phone'];
    	$order_billing_vat = $order->get_meta('_billing_piva');
        $order_billing_sdi = $order->get_meta('_billing_code_sdi');
    	
    	update_user_meta( $user_id, 'first_name', $order_billing_firstname ); 
    	update_user_meta( $user_id, 'last_name', $order_billing_lastname ); 
    	update_user_meta( $user_id, 'company', $order_billing_company ); 
    	update_user_meta( $user_id, 'vat', $order_billing_vat ); 
    	update_user_meta( $user_id, 'sdi', $order_billing_sdi ); 
    	update_user_meta( $user_id, 'telefono', $order_billing_phone ); 
      
    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Update usermeta after checkout’ is closed to new replies.