Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding user custom field value to order items details

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:

Order Details

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.

like image 721
Verse Avatar asked Mar 10 '23 00:03

Verse


1 Answers

There are multiple ways (updated for WooCommerce 3+):

1) The cleanest way (in 2 Steps):

Step A) 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:

enter image description here

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.

enter image description here

Then you will have this after saving and updating:

enter image description here

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 B) Your custom function hooked in woocommerce_checkout_create_order_line_item 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_checkout_create_order_line_item', 'add_custom_hiden_order_item_meta_data', 20, 4 );
function add_custom_hiden_order_item_meta_data( $item, $cart_item_key, $values, $order ) {

    // Set user meta custom field as order item meta
    if( $meta_value = get_user_meta( $order->get_user_id(), 'billing_enumber', true ) )
        $item->update_meta_data( 'pa_billing-e-number', $meta_value );
}

This code goes in functions.php file of your active child theme (or active theme). Tested and works.

Then on front end in My account > Orders > Order view, you will get this:

enter image description here

As you can see, you get exactly something similar at the WooCommerce normal behavior. The value here is just to illustrate this example…


2) The simplest way is to use a very similar code (without the Step B):

add_action('woocommerce_checkout_create_order_line_item', 'add_custom_hiden_order_item_meta_data', 20, 4 );
function add_custom_hiden_order_item_meta_data( $item, $cart_item_key, $values, $order ) {

    // Set user meta custom field as order item meta
    if( $meta_value = get_user_meta( $order->get_user_id(), 'billing_enumber', true ) )
        $item->update_meta_data( __('Billing E Number', 'woocommerce'), $meta_value );
}

This code goes in functions.php file of your active child theme (or active theme). Tested and works.

Then on front end in My account > Orders > Order view, you will get the same thing:

enter image description here

like image 149
LoicTheAztec Avatar answered Mar 20 '23 20:03

LoicTheAztec