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

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

问题描述

与供应商(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?

这是我会撒谎的截图:

配置文件自定义字段

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

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

谢谢.

推荐答案

有多种方式(针对 WooCommerce 3+ 更新):

1) 最干净的方式 (分 2 个步骤):

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

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.

在您的情况下,您将创建Billing E Number"属性:

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 中的属性 slug 以 pa_ 开头.所以你的属性 slug 将是: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)

步骤 B) 您的自定义函数挂在 woocommerce_checkout_create_order_line_item 动作挂钩中.

Step B) Your custom function hooked in woocommerce_checkout_create_order_line_item 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.

所以代码将很简单:

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

此代码位于活动子主题(或活动主题)的 functions.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…

<小时>

2) 最简单的方法是使用非常相似的代码(没有步骤 B):


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

此代码位于活动子主题(或活动主题)的 functions.php 文件中.经测试有效.

然后在我的账户 > 订单 > 订单视图的前端,你会得到同样的东西:

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

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

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