With the following code, I get a custom field on a WooCommerce product, but how can I display it on the order, checkout, and admin order (backend)?
This is my custom field on woocommerce per product
This is my admin order (backend) detail. I try to display the correct metadata that I entered in per product post.
How can I display the value I entered to my custom field into my admin order? I also hope somebody can help me to display the meta in the order and checkout page... or guide me to right direction. I realise that in order to display it in the admin order, I have to make sure that I register the meta to order or checkout first.
// Display Fields
add_action('woocommerce_product_options_general_product_data', 'woocommerce_product_custom_fields');
// Save Fields
add_action('woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save');
function woocommerce_product_custom_fields()
{
global $woocommerce, $post;
echo '<div class="product_custom_field">';
// Custom Product Text Field
woocommerce_wp_text_input(array(
'id' => '_custom_product_text_field',
'placeholder' => 'Custom Product Text Field',
'label' => __('Custom Product Text Field', 'woocommerce') ,
'desc_tip' => 'true'
));
// Custom Product Number Field
woocommerce_wp_text_input(array(
'id' => '_custom_product_number_field',
'placeholder' => 'Custom Product Number Field',
'label' => __('Custom Product Number Field', 'woocommerce') ,
'type' => 'number',
'custom_attributes' => array(
'step' => 'any',
'min' => '0'
)
));
// Custom Product Textarea
woocommerce_wp_textarea_input(array(
'id' => '_custom_product_textarea',
'placeholder' => 'Custom Product Textarea',
'label' => __('Custom Product Textarea', 'woocommerce')
));
echo '</div>';
}
function woocommerce_product_custom_fields_save($post_id)
{
// Custom Product Text Field
$woocommerce_custom_product_text_field = $_POST['_custom_product_text_field'];
if (!empty($woocommerce_custom_product_text_field)) update_post_meta($post_id, '_custom_product_text_field', esc_attr($woocommerce_custom_product_text_field));
// Custom Product Number Field
$woocommerce_custom_product_number_field = $_POST['_custom_product_number_field'];
if (!empty($woocommerce_custom_product_number_field)) update_post_meta($post_id, '_custom_product_number_field', esc_attr($woocommerce_custom_product_number_field));
// Custom Product Textarea Field
$woocommerce_custom_procut_textarea = $_POST['_custom_product_textarea'];
if (!empty($woocommerce_custom_procut_textarea)) update_post_meta($post_id, '_custom_product_textarea', esc_html($woocommerce_custom_procut_textarea));
}
// Display in order edit
add_action('woocommerce_admin_order_data_after_billing_address', 'display_verification_id_in_admin_order_meta', 10, 1);
function display_verification_id_in_admin_order_meta($order)
{
// compatibility with WC +3
$order_id = method_exists($order, 'get_id') ? $order->get_id() : $order->id;
$total_incart = number_format((float)$order->get_total() - $order->get_total_tax() - $order->get_total_shipping() - $order->get_shipping_tax() , wc_get_price_decimals() , '.', '');
$post_meta_value = get_post_meta($post->ID, '_custom_product_text_field', true);
echo '<p><strong>' . __('TEXTFIELD', 'woocommerce') . ':</strong> ' . get_post_meta($order_id, '_custom_product_text_field', true) . '</p>';
echo '<p><strong>' . __('NUMBERFIELD', 'woocommerce') . ':</strong> ' . get_post_meta($order_id, '_custom_product_number_field', true) . '</p>';
echo '<p><strong>' . __('TEXTAREAFIELD', 'woocommerce') . ':</strong> ' . get_post_meta($order_id, '_custom_product_textarea', true) . '</p>';
echo '<p><strong>Total price items: </strong>' . $total_incart . '</p>';
}