以管理员顺序显示Woocommerce自定义结帐字段值,使其可编辑

时间:2019-02-28 18:48:36

标签: php wordpress woocommerce metadata orders

我正在使用Show hide custom WooCommerce checkout field based on selected payment method回答我的一个问题,以显示/隐藏自定义结帐帐单字段,并且效果很好。

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

2 个答案:

答案 0 :(得分:1)

要在“帐单信息”列上的管理订单页面中显示“ 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>';
}

enter image description here

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

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

enter image description here

代码进入您的活动子主题(或活动主题)的function.php文件中。经过测试并可以正常工作。

答案 1 :(得分:0)

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 } ?>
相关问题