将用户自定义字段值添加到订单商品详细信息 [英] Adding user custom field value to order items details

查看:247
本文介绍了将用户自定义字段值添加到订单商品详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与供应商(WC供应商)一起开发WooCommerce网上商店.

Developing a WooCommerce webshop with vendors (WC Vendors).

我需要显示在供应商资料中创建的自定义字段.应该将其显示在 order-details.php 中的项目和供应商名称下.

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.

如何显示该卖方/卖方ID的个人资料字段?
有人可以帮助我吗?

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:

个人资料自定义字段

将自定义字段添加到用户个人资料页面

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
}

向用户个人资料上的自定义字段添加更新功能

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;
    }

谢谢.

推荐答案

您可以通过以下两个步骤以干净的方式完成此操作:

步骤1)您需要首先在产品中添加属性,以获取将作为订单显示的自定义字段值的可读标签" 项目元数据.

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:

然后woocommerce中的属性标签以 pa_ 开始.因此,您的属性标签将是: pa_billing-e-number

Then attributes slugs in woocommerce begin by pa_. So your attribute slug is going to be: pa_billing-e-number

我们将在下面的函数中使用它,以显示您的自定义字段值的可读标签.因此您将获得以下订单项: Billing E Number: (some value)

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)

步骤2)您的自定义功能已钩在woocommerce_add_order_item_meta动作钩子中.

STEP 2) Your custom function hooked in woocommerce_add_order_item_meta action hook.

现在要在订单商品详细信息中显示您的自定义字段,您将需要获取已提交的值以另存为订单商品元数据,我们将在此处使用 pa_billing-e-Number 作为 meta_key .

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);

}

此代码会出现在您活动的子主题(或主题)的function.php文件中,也可能会出现在任何插件文件中.

然后在前端的我的帐户">订单">订单"视图中,您将获得以下信息:

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

如您所见,您在woocommerce的正常行为中得到了完全相似的东西.此处的值仅用于说明此示例……

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

该示例已经过实际测试,可以正常工作.

这篇关于将用户自定义字段值添加到订单商品详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆