Developing a WooCommerce webshop with vendors (WC Vendors).
I need to display a custom field that I have created in vendors profile. It shoud be displayed under the item and vendor name in order-details.php
.
How to display profile field by that seller/vendor id?
Anybody could help me?
Here's a screenshot of what I would lie to have:
Profile Custom Fields
Adding custom fields to user profile page
add_action( 'show_user_profile', 'wp_added_user_profile_fields' );
function wp_added_user_profile_fields( $user ) {
?>
<table class="form-table">
<tr>
<th><label for="billing_enumber"><?php _e( "eNumber", 'woocommerce' ); ?></label></th>
<td>
<input type="text"
name="billing_enumber"
id="billing_enumber"
class="regular-text"
value="<?php echo esc_attr( get_the_author_meta( 'billing_enumber', $user->ID ) ); ?>"/>
<span class="description"><?php _e( 'Please enter your eNumber.', 'woocommerce' ); ?></span>
</td>
</tr>
</table>
<?php
}
Adding update function to custom fields on user profile
add_action( 'edit_user_profile', 'wp_added_user_profile_fields' );
function wp_save_added_user_profile_fields( $user_id ) {
if ( current_user_can( 'edit_user', $user_id ) ) {
update_user_meta( $user_id, 'billing_enumber', trim($_POST['billing_enumber'] ) );
$saved = true;
}
return true;
}
Thanks.
You can do it in a clean way in 2 Steps:
STEP 1) You will need first to add an attribute in your products to get a "readable label" for your custom field value that is going to appear as order items meta data.
In your case you will create "Billing E Number" attribute:
Then you will set it with any value (as it will be replaced by your custom field value) in your targeted products that can be simple or variable. If you don't set a value with this attribute, it will not get set and saved when updating the product.
Then you will have this after saving and updating:
Then attributes slugs in woocommerce begin by pa_
. So your attribute slug is going to be: pa_billing-e-number
We will use it in the function below, to display that readable label for your custom field value. so you will get in the order items: Billing E Number: (some value)
STEP 2) Your custom function hooked in woocommerce_add_order_item_meta
action hook.
Now to display your custom field in orders item details you will need to get that submitted value to save as order item meta data and we will use here pa_billing-e-Number
as meta_key
.
So the code will be simply something like:
add_action('woocommerce_add_order_item_meta', 'add_custom_order_item_meta_data', 1, 3 );
function add_custom_order_item_meta_data( $item_id, $values, $cart_item_key ) {
global $order;
// Get the user ID
$user_id = get_post_meta( $order->id, '_customer_user', true );
// Get User custom field value for 'billing_enumber'
$billing_e_number = get_user_meta( $user_id, 'billing_enumber', true );
// Setting this custom field in order item meta
if(!empty($billing_e_number))
wc_add_order_item_meta($item_id, 'pa_billing-e-number', $billing_e_number, true);
}
This code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Then on front end in My account > Orders > Order view, you will get this:
As you can see, you get exactly something similar at the woocommerce normal behavior. The value here is just to illustrate this example…
This example is really tested and works.