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