Woocommerce display custom field data on admin order details - php

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

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.

Related

How to save from WooCommerce checkout a custom checkbox field state?

I have a problem with the update_post_meta function.
I have a user submitted value, which I pass via $_POST and then saving to post meta.
All is working fine, but when the value is '0' the post meta is not updated.
This is My code:
// Add custom checkout field: woocommerce_review_order_before_submit
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field_ritiro_sede' );
function my_custom_checkout_field_ritiro_sede() {
echo '<div class="cw_custom_class"><h3>'.__('Ritiro presso sede CER S.r.l. &nbsp').'</h3>';
echo '<div id="my_custom_checkout_field">';
woocommerce_form_field( 'ritiro_sede', array(
'type' => 'checkbox',
'class' => array('input-checkbox'),
'label' => __('SI'),
), WC()->checkout->get_value( 'ritiro_sede' ) );
echo '</div>';
}
// Save the custom checkout field in the order meta, when checkbox has been checked
add_action( 'woocommerce_checkout_update_order_meta', 'custom_checkout_field_update_order_meta_ritiro_sede', 10, 1 );
function custom_checkout_field_update_order_meta_ritiro_sede( $order_id ) {
if ( ! empty( $_POST['ritiro_sede'] ) )
update_post_meta( $order_id, 'ritiro_sede', $_POST['ritiro_sede'] );
if ( isset( $_POST['ritiro_sede'] ) )
update_post_meta( $order_id, 'ritiro_sede', $_POST['0'] );
}
Does anyone have any idea what might be wrong?
Since WooCommerce 3, here below is the best way to save your custom checkout checkbox field value as order meta data (including when the checkbox is unchecked):
// Save the custom checkout checkbox field as the order meta
add_action( 'woocommerce_checkout_create_order', 'custom_checkout_field_update_order_meta', 10, 2 );
function custom_checkout_field_update_order_meta( $order, $data ) {
$value = isset($_POST['ritiro_sede']) ? '1' : '0'; // Set the correct values
$order->update_meta_data( 'ritiro_sede', $value );
}
Now as user meta data is used by WC_Checkout get_value() method in your first function on:
WC()->checkout->get_value( 'ritiro_sede' )
So if you want the submitted value to be displayed on checkout page for the next purchase, you will need to save that custom checkout field also as user meta data using instead the following:
// Save the custom checkout checkbox field as the order meta and user meta
add_action( 'woocommerce_checkout_create_order', 'custom_checkout_field_update_order_meta', 10, 2 );
function custom_checkout_field_update_order_meta( $order, $data ) {
$value = isset($_POST['ritiro_sede']) ? '1' : '0'; // Set the correct values
// Save as custom order meta data
$order->update_meta_data( 'ritiro_sede', $value );
// Save as custom user meta data
if ( get_current_user_id() > 0 ) {
update_user_meta( get_current_user_id(), 'ritiro_sede', $value );
}
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.

Save product custom-field as custom order item metadata for WooCommerce admin manual orders

Using Pass custom product meta data to the order in Woocommerce 3 answer code, Is it possible to save and display custom metadata when the product is added manually from backend when creating orders manually from the backend?
That is my code (lightly changed):
// Admin products: Display custom Field
add_action( 'woocommerce_product_options_general_product_data', 'product_options_general_product_data_add_field' );
function product_options_general_product_data_add_field() {
global $post;
echo '<div class="options_group">';
woocommerce_wp_select( array(
'id' => '_cost_centre',
'label' => __( 'Cost Centre', 'woocommerce' ),
'options' => array(
'MFEG' => __( 'MFEG', 'woocommerce' ), // Default displayed option value
'YDIT' => __( 'YDIT', 'woocommerce' ),
)
) );
echo '</div>';
}
// Admin products: Save custom Field
add_action( 'woocommerce_process_product_meta', 'product_options_general_product_data_save_field' );
function product_options_general_product_data_save_field( $post_id ){
if( isset( $_POST['_cost_centre'] ) )
update_post_meta( $post_id, '_cost_centre', esc_attr( $_POST['_cost_centre'] ) );
}
// Order items: Save product "Cost centre" as hidden order item meta data
add_action('woocommerce_checkout_create_order_line_item', 'save_file_type_as_order_item_meta', 20, 4);
function save_file_type_as_order_item_meta($item, $cart_item_key, $values, $order) {
if ( $cost_centre = $values['data']->get_meta('_cost_centre') ) {
$item->update_meta_data( '_cost_centre', $cost_centre ); // Save as order item (visble on admin only)
}
}
This works fine when the order is created by the client from the frontend. But when admin creates an order manually from the backend and adds the product, the custom metadata is not visible.
How to solve this problem for orders created manually, allowing to add product custom field as custom order item data?
Update 3
For manual backend orders, you can try to use woocommerce_before_save_order_item dedicated action hook as follow (code based on your question code):
add_action( 'woocommerce_before_save_order_item', 'action_before_save_order_item_callback' );
function action_before_save_order_item_callback( $item ) {
$cost_centre = $item->get_meta('_cost_centre');
// If custom meta data is not saved as order item
if ( empty($cost_centre) ) {
// Get custom meta data from the product
$cost_centre = get_post_meta( $item->get_product_id(), '_cost_centre', true );
$cost_centre = empty($cost_centre) ? 'MFEG' : $cost_centre;
// Save it as custom order item (if defined)
$item->update_meta_data( '_cost_centre', $cost_centre );
}
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
Addition: Make the order item custom meta data visible to customer
If you want this order item meta data to be visible on customer orders and email notifications, you will replace the order item meta key from '_cost_centre' to 'Cost centre' as follow:
add_action( 'woocommerce_before_save_order_item', 'action_before_save_order_item_callback' );
function action_before_save_order_item_callback( $item ) {
$cost_centre = $item->get_meta('_cost_centre');
// If custom meta data is not saved as order item
if ( empty($cost_centre) ) {
// Get custom meta data from the product
$cost_centre = get_post_meta( $item->get_product_id(), 'Cost centre', true );
$cost_centre = empty($cost_centre) ? 'MFEG' : $cost_centre;
// Save it as custom order item (if defined)
$item->update_meta_data( 'Cost centre', $cost_centre );
}
}
This time it will be visible on customer orders and emails.
You will need also to change your last function on your question code to:
// Order items: Save product "Cost centre" as visible order item meta data
add_action('woocommerce_checkout_create_order_line_item', 'save_file_type_as_order_item_meta', 20, 4);
function save_file_type_as_order_item_meta($item, $cart_item_key, $values, $order) {
if ( $cost_centre = $values['data']->get_meta('_cost_centre') ) {
$item->update_meta_data( 'Cost centre', $cost_centre ); // Save as order item (visible everywhere)
}
}
Note: When the order item custom meta key starts with an underscore, it's hidden.

Show Woocommerce custom checkout field value in admin order making them editable

I am using "Show hide custom WooCommerce checkout field based on selected payment method" answer to one of my questions, to show / hide a custom checkout billing field, and it works fine.
Question: Is it possible to show my Custom field in WooCommerce orders in the admin panel?
To display "billing_options" custom checkout billing field value in admin order pages on the billing information column, use the following:
add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_billing_options_value_in_admin_order', 10, 1 );
function display_billing_options_value_in_admin_order($order){
if( $value = get_post_meta( $order->get_id(), '_billing_options', true ) )
echo '<p><strong>'.__('Invoice Number', 'woocommerce').':</strong> ' . $value . '</p>';
}
To make this custom checkout billing field appear as editable in backend use the following:
add_filter( 'woocommerce_admin_billing_fields', 'custom_admin_billing_fields', 10, 1 );
function custom_admin_billing_fields( $fields ) {
$fields['options'] = array(
'label' => __('Invoice Number', 'woocommerce'),
'show' => true,
);
return $fields;
}
Code goes in function.php file of your active child theme (or active theme). tested and works.
add_action( 'woocommerce_admin_order_data_after_order_details', 'mycustom_order_meta_general' );
function mycustom_order_meta_general( $order ){ ?>
<br class="clear" />
<h4>Gift Order Edit</h4>
<?php
/*
* get all the meta data values we need
*/
$_mycustomfield = get_post_meta( $order->get_id(), '_mycustomfield', true );
?>
<div class="address">
<p><strong>My Custom Field</strong></p>
<?php
if( $_mycustomfield ) :
?>
<p><strong>MY custom:</strong> <?php echo $_mycustomfield ?></p>
<?php
endif;
?>
</div>
<?php } ?>

Display custom payment field in Woocommerce Admin, Orders and emails

I need to display my custom checkout fields in admin orders page, thank_you and email notifications.
I am using Validate and save additional checkout field for specific payment gateway in Woocommerce answer code to show, validate and save my custom field.
From Woocommerce display custom field data on admin order details I am trying to display my custom field saved inputted value.
This is the code I have so far:
// Display field value on the order edit page
add_action( 'woocommerce_admin_order_data_after_billing_address',
'show_Ean_nummer_in_admin', 10, 1 );
function show_Ean_nummer_in_admin ( $order ){
// Get "ean" custom field value
$udfyld_ean = get_post_meta( $order_id, '_udfyld_ean', true );
// Display "ean" custom field value
echo '<p>'.__('EAN', 'woocommerce') . $udfyld_ean . '</p>';
}
// Display field value on the order emails
add_action( 'woocommerce_email_order_details', 'ean_in_emails' 50, 1 );
function ean_in_emails( $order, $sent_to_admin, $plain_text, $email ){
// Get "ean" custom field value
$udfyld_ean = get_post_meta( $order_id, '_udfyld_ean', true );
// Display "ean" custom field value
echo '<p>'.__('EAN', 'woocommerce') . $udfyld_ean . '</p>';
}
// Display field value in thank you
add_action( 'woocommerce_thankyou', 'ean_in_thankyou' );
function ean_in_thankyou() {
// Get "ean" custom field value
$udfyld_ean = get_post_meta( $order_id, '_udfyld_ean', true );
// Display "ean" custom field value
echo '<p>'.__('EAN', 'woocommerce') . $udfyld_ean . '</p>';
}
But It's not working. The field does get appended to the database, but does not display anywhere:
How can I display the field properly?
The following code will display your "Udfyld EAN" custom field value in orders and email notifications:
1) To display that in Woocommerce order admin single pages:
// Display field value on the admin order edit page
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'custom_field_admin_display_order_meta', 10, 1 );
function custom_field_admin_display_order_meta( $order ){
$business_address = get_post_meta( $order->get_id(), 'Business Address?', true );
if( $udfyld_ean = $order->get_meta('_udfyld_ean') )
echo '<p><strong>'.__('Udfyld EAN', 'woocommerce').': </strong> ' . $udfyld_ean . '</p>';
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
2) To display that on Order received, Order view and email notifications you will use:
add_filter( 'woocommerce_get_order_item_totals', 'add_udfyld_ean_row_to_order_totals', 10, 3 );
function add_udfyld_ean_row_to_order_totals( $total_rows, $order, $tax_display ) {;
$new_total_rows = [];
foreach($total_rows as $key => $total ){
$new_total_rows[$key] = $total;
if( $order->get_meta('_udfyld_ean') && 'payment_method' === $key ){
$new_total_rows['udfyld_ean'] = array(
'label' => __('Udfyld EAN', 'woocommerce'),
'value' => esc_html( $order->get_meta('_udfyld_ean') ),
);
}
}
return $new_total_rows;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
You are using following code to fetch the value of _udfyld_ean :
get_post_meta( $order_id, '_udfyld_ean', true );
But the problem is, you have not defined $order_id anywhere. You will need to pass valid value of order ID to get the expected output.

Insert a custom field value as a new row on email order totals table in Woocommerce

I'm helping my mother create her website for her store i Denmark.
It has gone okay, but now i'm stuck with a problem.
I need to be able to make a custom field in the order complete email, that displays a track & trace number, i've inserted on the admin order page. Everything i've done up until now, haven't worked, so therefore i seek your help.
I've added a custom field already called Track & Trace Pakkenr. (see screenshot 1)
But the problem is getting this in the order complete email, under shipping (forsendelse in danish, see screenshot 2)
I am also a complete and utter beginner in coding so if any of you can give any help or advice, please make it almost foolproof to follow.
Here are the screenshot 1 and screenshot 2.
To get this order custom field value displayed in order totals table on email notifications, use the following:
add_filter( 'woocommerce_get_order_item_totals', 'insert_custom_line_order_item_totals', 10, 3 );
function insert_custom_line_order_item_totals( $total_rows, $order, $tax_display ){
// Only on emails notifications
if( is_wc_endpoint_url() ) return $total_rows; // Exit
$tracking_label = 'Track & Trace Pakkenr.'; // The tracking label name
$tracking_value = $order->get_meta( $tracking_label ); // Get the tracking value (custom field).
if( empty($tracking_value) ) return $total_rows; // Exit
$new_total_rows = array(); // Initializing
// Loop through total rows
foreach( $total_rows as $key => $value ){
if( 'payment_method' == $key && ! empty($tracking_value) ) {
$new_total_rows['tracking_parcel'] = array(
'label' => $tracking_label,
'value' => $tracking_value,
);
}
$new_total_rows[$key] = $total_rows[$key];
}
return sizeof($new_total_rows) > 0 ? $new_total_rows : $total_rows;
}
Code goes in function.php file of your active child theme (or active theme). tested and works.
You will get something like:
From this:
You can add custom fields to the order email, you need to use the field key where the code has meta_key.
/**
* Add a custom field (in an order) to the emails
*/
add_filter( 'woocommerce_email_order_meta_fields', 'custom_woocommerce_email_order_meta_fields', 10, 3 );
function custom_woocommerce_email_order_meta_fields( $fields, $sent_to_admin, $order ) {
$fields['meta_key'] = array(
'label' => __( 'Label' ),
'value' => get_post_meta( $order->id, 'meta_key', true ),
);
return $fields;
}
Ref: https://docs.woocommerce.com/document/add-a-custom-field-in-an-order-to-the-emails/
Below is a simple yet functional example of adding custom meta fields to order email
add_filter( 'woocommerce_email_order_meta_fields', 'woocommerce_email_order_meta_fields_func', 10, 3 );
function woocommerce_email_order_meta_fields_func( $fields, $sent_to_admin, $order ) {
$fields['Track_Field'] = array(
'label' => __( 'Track', 'woocommerce' ),
'value' => wptexturize( get_post_meta( $order->id, 'Track_Field', true ) )
);
//... more meta fields goes here
return $fields;
}
In email after order email table:
add_action( 'woocommerce_email_after_order_table', 'woocommerce_email_after_order_table_func' );
function woocommerce_email_after_order_table_func( $order ) {
?>
<h3>Track</h3>
<table>
<tr>
<td>Track Fields </td>
<td><?php echo wptexturize( get_post_meta( $order->id, 'Track_Field', true ) ); ?></td>
</tr>
<!--additional custom meta and HTML code goes here-->
</table>
<?php
}
For more help see this link : Click here

Categories