在管理员订单中显示 Woocommerce 自定义结帐字段值,使其可编辑

Show Woocommerce custom checkout field value in admin order making them editable

我正在使用 "" 来回答我的一个问题,以显示/隐藏自定义结账账单字段,它工作正常。

问题:是否可以在管理面板的 WooCommerce 订单中显示我的自定义字段?

add_action( 'woocommerce_admin_order_data_after_order_details', 'mycustom_order_meta_general' );

function mycustom_order_meta_general( $order ){  ?>

        <br class="clear" />
        <h4>Gift Order <a href="#" class="edit_address">Edit</a></h4>
        <?php 
            /*
             * get all the meta data values we need
             */ 
            $_mycustomfield = get_post_meta( $order->get_id(), '_mycustomfield', true );

        ?>
        <div class="address">
            <p><strong>My Custom Field</strong></p>
            <?php

                if( $_mycustomfield ) : 
                ?>
                    <p><strong>MY custom:</strong> <?php echo $_mycustomfield ?></p>

                <?php
                endif;
            ?>
        </div>
<?php } ?>

要在管理订单页面的账单信息栏中显示 "billing_options" 自定义结帐账单字段值,请使用以下内容:

add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_billing_options_value_in_admin_order', 10, 1 );
function display_billing_options_value_in_admin_order($order){
    if( $value = get_post_meta( $order->get_id(), '_billing_options', true ) )
        echo '<p><strong>'.__('Invoice Number', 'woocommerce').':</strong> ' . $value . '</p>';
}

要使此自定义结帐账单字段在后端显示为可编辑,请使用以下命令:

add_filter( 'woocommerce_admin_billing_fields', 'custom_admin_billing_fields', 10, 1 );
function custom_admin_billing_fields( $fields ) {
    $fields['options'] = array(
        'label' => __('Invoice Number', 'woocommerce'),
        'show'  => true,
    );
    return $fields;
}

代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。