1

I'm not sure what I'm doing wrong but I'm trying to add Metadata that my resellers enter and save on their profiles to my orders. Resellers enter VAT number which is saved as metadata on the reseller's user profile.

I'm trying to get this metadata to show as order metadata. Any idea what I am doing wrong?


// Add VAT Number as Order Meta Data

add_action( 'woocommerce_checkout_create_order', 'wwp_customer_meta_data', 10, 2 );
function wwp_customer_meta_data( $order, $data ) {
    $user_id  = $order->get_user_id(); // Get the user id
    $wwp_vat = get_user_meta( $user_id, 'wwp_wholesaler_tax_id', true );
    $order->update_meta_data( 'VAT Number', $wwp_vat );
}
  • How is `$post_id` defined? – Howard E Aug 15 '21 at 16:58
  • I've updated my code but I still can't seem to get the VAT number to show as meta. Not sure what I'm doing wrong here... – Wanderlust Consulting Aug 16 '21 at 19:52
  • How is `wwp_wholesaler_tax_id` getting posted to the checkout? – Howard E Aug 16 '21 at 19:55
  • It's not posted on checkout. It's already saved to the customer profile when they create a reseller account. So the data is already on the user account. I'm trying to add user metadata as order metadata. – Wanderlust Consulting Aug 16 '21 at 20:22
  • Try using woocommerce_thankyou hook instead. Your latest code version should work – Howard E Aug 17 '21 at 00:01
  • Look at this answer: https://stackoverflow.com/a/48502896/3821467 – Howard E Aug 17 '21 at 09:33
  • So my code actually does work. It just doesn't work when you create an order from the order admin page. But if you create an order from the front end (as customer), it works just fine. – Wanderlust Consulting Aug 17 '21 at 09:53
  • By the way, your suggestion of using the woocommerce_thankyou hook, created an error on the thank you page. This is how you would do it using woocommerce_thankyou: https://stackoverflow.com/questions/41388921/adding-user-custom-field-to-the-order-meta-data-automatically/41389064#41389064 – Wanderlust Consulting Aug 17 '21 at 09:54
  • the thankyou hook only passes the orderid. So you'd have to get the order object from that. – Howard E Aug 17 '21 at 13:12

0 Answers0