douqing5981
2017-10-19 15:24
浏览 61
已采纳

Woocommerce显示管理订单详细信息的自定义字段数据

I am using a custom checkout field to give my customers a 'Ship to a business address' option on the checkout page of my woocommerce store. Most of the code is working properly, but I am unable to display whether or not they checked the box in the admin order details in the back end.

I have added a custom checkout field to my woocommerce shop, and saved the data to the order meta:

//add custom checkout field
add_filter( 'woocommerce_after_checkout_billing_form', 'gon_business_address_checkbox_field' );

function gon_business_address_checkbox_field( $checkout ){
    woocommerce_form_field( 'business_address_checkbox', array(
        'label'       => __('<h3 id="business_address_label">Check this box if you are shipping to a business.</h3>', 'woocommerce'),
        'required'    => false,
        'clear'       => false,
        'type'        => 'checkbox'
    ), $checkout->get_value( 'business_address_checkbox' ));
}


//update order meta
add_action('woocommerce_checkout_update_order_meta', 'gon_update_order_meta_business_address');

function gon_update_order_meta_business_address( $order_id ) {
    if ($_POST['business_address_checkbox']) update_post_meta( $order_id, 'Business Address?', 
    esc_attr($_POST['business_address_checkbox']));
}

Here's where I attempt to display this data on the admin order section. I have followed the previous topics on this as closely as possible, but to no avail.

// Display field value on the admin order edit page
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );

function my_custom_checkout_field_display_admin_order_meta($order){
    echo '<p><strong>'.__('Ship to a Business Address', 'woocommerce').': </strong> ' . get_post_meta( $order->get_id(), '_business_address_checkbox', true ) . '</p>';
}

Is this issue possibly because I'm not using the checkbox in the correct way? The peculiar thing is that I am getting the info to print on the order emails as I wish by using this code:

add_filter( 'woocommerce_email_order_meta_keys', 'my_custom_checkout_field_order_meta_keys' );
function my_custom_checkout_field_order_meta_keys( ) {
    if($_POST['business_address_checkbox']){
        $ship_to = 'YES';
    } else {
        $ship_to = 'NO';
    }
    echo '<h3>Ship to a business address? : '.$ship_to.'</h3>';
}
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题