Woocommerce 3 中可编辑的管理员自定义计费字段错误问题

Але*_*ова 2 php wordpress metadata orders woocommerce

我在此代码中有一个错误(按顺序添加编辑页面可编辑的自定义计费字段):

add_filter( 'woocommerce_admin_billing_fields' , 'order_admin_custom_fields' );
function order_admin_custom_fields( $fields ) {
    global $theorder;

    $fields['billing_address_3'] = array(
        'label' => __( 'Home', 'woocommerce' ),
        'value'=> get_post_meta( $theorder->get_id(), 'Home', true ),
        'show'  => true,
        'wrapper_class' => 'form-field-wide',
        'style' => '',
    );

    $fields['billing_address_4'] = array(
        'label' => __( 'Entrance', 'woocommerce' ),
        'value'=> get_post_meta( $theorder->get_id(), 'Entrance', true ),
        'show'  => true,
        'wrapper_class' => 'form-field-wide',
        'style' => '',
    );

    $fields['billing_address_5'] = array(
        'label' => __( 'Floor', 'woocommerce' ),
        'value'=> get_post_meta( $theorder->get_id(), 'Floor', true ),
        'show'  => true,
        'wrapper_class' => 'form-field-wide',
        'style' => '',
    );

    return $fields;
}
Run Code Online (Sandbox Code Playgroud)

犯罪行: 'value'=> get_post_meta( $theorder->get_id(), 'Home', true ),

报告错误:

example.com [Wed Jul 04 02:36:28 2018] [error] [pid 148187] sapi_apache2.c(362): [client 37.146.123.6:33708] PHP Fatal error:  Uncaught Error: Call to a member function get_id() on null in /home/c/cb36070/example.com/public_html/wp-content/themes/theme-name/functions.php:607\nStack trace:\n#0 /home/c/cb36070/example.com/public_html/wp-includes/class-wp-hook.php(286): order_admin_custom_fields(Array)\n#1 /home/c/cb36070/example.com/public_html/wp-includes/plugin.php(203): WP_Hook->apply_filters(Array, Array)\n#2 /home/c/cb36070/example.com/public_html/wp-content/plugins/woocommerce/includes/admin/meta-boxes/class-wc-meta-box-order-data.php(87): apply_filters('woocommerce_adm...', Array)\n#3 /home/c/cb36070/example.com/public_html/wp-content/plugins/woocommerce/includes/admin/meta-boxes/class-wc-meta-box-order-data.php(526): WC_Meta_Box_Order_Data::init_address_fields()\n#4 /home/c/cb36070/example.com/public_html/wp-includes/class-wp-hook.php(286): WC_Meta_Box_Order_Data::save(951, Object(WP_Post))\n#5 /home/c/cb36070/example.com/public_html/wp-includes/class-wp-hook.php(310): WP_Hook->apply_filters(NULL, Array)\n#6 /home/c/cb360 in /home/c/cb36070/example.com/public_html/wp-content/themes/theme-name/functions.php on line 607
Run Code Online (Sandbox Code Playgroud)

但我需要为那些自定义计费字段获取保存的值。

如何解决此错误问题并获取自定义计费字段值?

添加(保存)的元数据是用以下代码制作的:

add_action( 'woocommerce_checkout_update_order_meta', 'custom_checkout_field_update_order_meta' );
function custom_checkout_field_update_order_meta( $order_id ) {
    if ( ! empty( $_POST['billing_address_3'] ) ) {
        update_post_meta( $order_id, 'Home', sanitize_text_field( $_POST['billing_address_3'] ) );
    }

    if ( ! empty( $_POST['billing_address_4'] ) ) {
        update_post_meta( $order_id, 'Entrance', sanitize_text_field( $_POST['billing_address_4'] ) );
    }

    if ( ! empty( $_POST['billing_address_5'] ) ) {
        update_post_meta( $order_id, 'Floor', sanitize_text_field( $_POST['billing_address_5'] ) );
    }
}
Run Code Online (Sandbox Code Playgroud)

任何帮助表示赞赏。

Loi*_*tec 7

问题是它$theorder没有定义,你不能get_id()在它上面使用方法。

但是您代码中的主要问题首先来自您的结帐帐单字段。它们应该设置、显示和保存如下(特别注意meta_keys保存数据时要使用的)

// Frontend: Display the custom billing fields (in checkout and my account)
add_filter( 'woocommerce_billing_fields' ,'add_custom_billing_fields', 20, 1 );
function add_custom_billing_fields( $fields ) {

    $fields['billing_address_3'] = array(
        'label' => __( 'Home', 'woocommerce' ),
        'placeholder'   => _x('Fill in your Home', 'placeholder', 'woocommerce'),
        'required'  => true,
        'class'     => array('form-row-wide'),
        'clear'     => true
    );

    $fields['billing_address_4'] = array(
        'label' => __( 'Entrance', 'woocommerce' ),
        'placeholder'   => _x('Fill in your Entrance', 'placeholder', 'woocommerce'),
        'required'  => true,
        'class'     => array('form-row-wide'),
        'clear'     => true
    );

    $fields['billing_address_5'] = array(
        'label' => __( 'Floor', 'woocommerce' ),
        'placeholder'   => _x('Fill in your Floor', 'placeholder', 'woocommerce'),
        'required'  => true,
        'class'     => array('form-row-wide'),
        'clear'     => true
    );

    return $fields;
}

// Save the custom billing fields (once order is placed)
add_action( 'woocommerce_checkout_create_order', 'save_custom_billingt_fields', 20, 2 );
function save_custom_billingt_fields( $order, $data ) {
    if ( isset( $_POST['billing_address_3'] ) && ! empty( $_POST['billing_address_3'] ) ) {
        $order->update_meta_data('_billing_address_3', sanitize_text_field( $_POST['billing_address_3'] ) );
        update_user_meta( $order->get_customer_id(), 'billing_address_3', sanitize_text_field( $_POST['billing_address_3'] ) );
    }
    if ( isset( $_POST['billing_address_4'] ) && ! empty( $_POST['billing_address_4'] ) ) {
        $order->update_meta_data('_billing_address_4', sanitize_text_field( $_POST['billing_address_4'] ) );
        update_user_meta( $order->get_customer_id(), 'billing_address_4', sanitize_text_field( $_POST['billing_address_4'] ) );
    }
    if ( isset( $_POST['billing_address_5'] ) && ! empty( $_POST['billing_address_5'] ) ) {
        $order->update_meta_data('_billing_address_5', sanitize_text_field( $_POST['billing_address_5'] ) );
        update_user_meta( $order->get_customer_id(), 'billing_address_5', sanitize_text_field( $_POST['billing_address_5'] ) );
    }
}
Run Code Online (Sandbox Code Playgroud)

您将看到这些自定义字段也在我的帐户 > 地址 > 编辑帐单地址中。所有相关的都是自动同步。无需额外的验证或保存代码...

现在在管理订单页面中,对于woocommerce_admin_billing_fields管理钩子,'value'字段数组中不存在键,它是有罪的

由于您已正确设置并保存了自定义结帐字段,因此您不需要任何'value'数组键,因为数据将自动填充(如果存在)。所以你的代码将是:

// Backend: Display editable custom billing fields
add_filter( 'woocommerce_admin_billing_fields' , 'order_admin_custom_fields' );
function order_admin_custom_fields( $fields ) {
    global $the_order;

    $fields['address_3'] = array(
        'label' => __( 'Home', 'woocommerce' ),
        'show'  => true,
        'wrapper_class' => 'form-field-wide',
        'style' => '',
    );

    $fields['address_4'] = array(
        'label' => __( 'Entrance', 'woocommerce' ),
        'show'  => true,
        'wrapper_class' => 'form-field-wide',
        'style' => '',
    );

    $fields['address_5'] = array(
        'label' => __( 'Floor', 'woocommerce' ),
        'show'  => true,
        'wrapper_class' => 'form-field-wide',
        'style' => '',
    );

    return $fields;
}
Run Code Online (Sandbox Code Playgroud)

代码位于活动子主题(或活动主题)的 function.php 文件中。测试和工作。

错误现在完全消失了