php - 将用户自定义字段值添加到订单项目详细信息

标签 php wordpress woocommerce custom-fields orders

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

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

如何通过该卖家/供应商 ID 显示个人资料字段?
有人可以帮助我吗?

这是我想撒谎的截图:

Order Details

配置文件自定义字段

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

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) 最干净的方式 (分两步):

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”属性:

enter image description here

然后您将在您的目标产品中将其设置为任何值(因为它将被您的自定义字段值替换),可以是简单的或可变的。如果您不使用此属性设置值,则在更新产品时不会设置和保存它。

enter image description here

保存更新后你会得到这个:

enter image description here

然后 woocommerce 中的属性 slug 以 pa_ 开头。所以你的属性 slug 将是:pa_billing-e-number

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

Step B) Your custom function hooked in woocommerce_checkout_create_order_line_item action hook.

现在要在订单商品详情中显示您的自定义字段,您需要获取提交的值以保存为订单商品元数据,我们将在此处使用pa_billing-e-Number 作为 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 文件中。经过测试并有效。

然后在“我的帐户”>“订单”>“订单” View 的前端,您将看到:

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) 最简单的方法是使用非常相似的代码(没有步骤 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 文件中。经过测试并有效。

然后在“我的帐户”>“订单”>“订单” View 中的前端,您将获得相同的内容:

enter image description here

关于php - 将用户自定义字段值添加到订单项目详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41981493/

相关文章:

ios - 带有 Woocommerce 的 FlexSlider : prevent vertical swipe touch on horizontal sliders

javascript - 使用此代码未收到 PHP 邮件

php - MySQL 查询 - 选择所有帖子并计算每个帖子的评论

wordpress - 从wp-config.php文件中提取数据库名称

wordpress get_the_category 返回空

css - 如何将我的 Woocommerce 产品图片对齐到顶部?

php - 在 woocommerce 结账时为我的自定义支付插件执行自定义 js 代码

javascript - 如何在 javascript 中使用服务器端倒数计时器?

php - PDO 发出带有 LIMIT 的语法错误

wordpress - 让 jQuery 验证插件与 WordPress 表单配合使用