peanutSquiggle
peanutSquiggle

Reputation: 55

Woocommerce display custom field data on admin order details

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

Upvotes: 4

Views: 5982

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 254378

As you are saving this custom field data, using the meta_key: 'Business Address?'… So you need to use this meta_key to retrieve the data this way:

// Display field value on the admin order edit page
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'custom_checkout_field_display_admin_order_meta', 10, 1 );
function custom_checkout_field_display_admin_order_meta( $order ){
    $business_address = get_post_meta( $order->get_id(), 'Business Address?', true );
    if( ! empty( $business_address ) )
        echo '<p><strong>'.__('Ship to a Business Address', 'woocommerce').': </strong> ' . $business_address . '</p>';
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

Tested on WooCommerce 3 and works.

Upvotes: 5

Related Questions