【问题标题】:Displaying custom checkout field value in Woocommerce admin order pages在 Woocommerce 管理订单页面中显示自定义结帐字段值
【发布时间】:2019-04-14 17:11:49
【问题描述】:

在 Woocommerce 中,我使用了来自 @LoicTheAztec 的 Add a new custom checkout field before billing details in Woocommerce?,效果很好。

我在管理订单页面中显示信息时遇到问题。

然后我尝试使用Add order metadata to WooCommerce admin order overview,对代码进行一些更改以满足我的需要:

// display the extra data in the order admin panel
function kia_display_order_data_in_admin( $order ){  ?>
    <div class="order_data_column">
        <h4><?php _e( 'Extra Details' ); ?></h4>
        <?php 
            echo '<p><strong>' . __( 'Some field' ) . ':</strong>' . get_post_meta( $order->id, '_my_field_name', true ) . '</p>';?>
    </div>
<?php }
add_action( 'woocommerce_admin_order_data_after_order_details', 'kia_display_order_data_in_admin' );?>

Extra DetailsSome field: 出现,但不显示自定义字段值。

我仍在试图找出我在这里做错了什么。关于我应该在哪里看的任何建议?

【问题讨论】:

    标签: php wordpress woocommerce backend orders


    【解决方案1】:

    更新 3: 尝试以下稍微重新访问的代码:

    add_action( 'woocommerce_checkout_before_customer_details', 'custom_checkout_fields_before_billing_details', 20 );
    function custom_checkout_fields_before_billing_details(){
        $domain = 'woocommerce';
        $checkout = WC()->checkout;
    
        echo '<div id="my_custom_checkout_field">';
    
        echo '<h3>' . __('My New Fields Section') . '</h3>';
    
        woocommerce_form_field( '_my_field_name', array(
            'type'          => 'text',
            'label'         => __('My 1st new field', $domain ),
            'placeholder'   => __('Please fill in "my 1st new field"', $domain ),
            'class'         => array('my-field-class form-row-wide'),
            'required'      => true, // or false
            ), $checkout->get_value( '_my_field_name' ));
    
        echo '</div>';
    }
    
    // Custom checkout fields validation
    add_action( 'woocommerce_checkout_process', 'custom_checkout_field_process' );
    function custom_checkout_field_process() {
        if ( isset($_POST['_my_field_name']) && empty($_POST['_my_field_name']) )
            wc_add_notice( __( 'Please fill in "My 1st new field".' ), 'error' );
    }
    
    // Save custom checkout fields the data to the order
    add_action( 'woocommerce_checkout_create_order', 'custom_checkout_field_update_meta', 10, 2 );
    function custom_checkout_field_update_meta( $order, $data ){
        if( isset($_POST['_my_field_name']) && ! empty($_POST['_my_field_name']) )
            $order->update_meta_data( '_my_field_name', sanitize_text_field( $_POST['_my_field_name'] ) );
    }
    
    // Display the extra data in the order admin panel
    add_action( 'woocommerce_admin_order_data_after_order_details', 'display_order_data_in_admin', 10, 1 );
    function display_order_data_in_admin( $order ){
        if( $value = $order->get_meta( '_my_field_name' ) ) {
            echo '<div class="order_data_column">
            <h4>' . __( "Extra Details", "woocommerce" ) . '</h4>
            <p><strong>' . __( 'Some field', "woocommerce" ) . ':</strong> ' . $value . '</p>
            </div>';
        }
    }
    

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

    【讨论】:

    • 我用这个替换了代码,一切看起来都很好,但管理员订单页面上什么也没有出现——它现在看起来完全有货。我将尝试搜索数据库中的帖子数据,以查看我输入该字段的信息是否已被保存 - 也许我的安装很不稳定
    • @J.Frenk 刚刚重新更新 (3),我在代码中做了一些其他更改 (更改了所有 meta_keys 开始时带有下划线,因为它是自定义字段的最佳方式) ...请再试一次。如果这个答案是回答你的问题,你可以请accept the answer,谢谢。
    猜你喜欢
    • 2018-10-01
    • 1970-01-01
    • 2020-08-03
    • 1970-01-01
    • 1970-01-01
    • 2021-04-12
    • 2021-04-17
    • 2021-11-10
    • 2021-06-26
    相关资源
    最近更新 更多