编程语言
首页 > 编程语言> > php – 添加用户自定义字段值以订购商品详细信息

php – 添加用户自定义字段值以订购商品详细信息

作者:互联网

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

谢谢.

解决方法:

您可以通过2个步骤以干净的方式完成:

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.

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

enter image description here

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

enter image description here

然后,您将在保存和更新后获得此信息:

enter image description here

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

我们将在下面的函数中使用它,以显示自定义字段值的可读标签.所以你会得到订单项目:Billing E Number :(某个值)

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

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

因此代码将简单地类似于:

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文件中,或者也可以放在任何插件文件中.

然后在我的帐户的前端>订单>订单视图,你会得到这个:

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…

这个例子真的经过测试并且有效.

标签:orders,custom-fields,php,wordpress,woocommerce
来源: https://codeday.me/bug/20190823/1701507.html