Display custom payment field in Woocommerce Admin, Orders and emails - php

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.

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.

Display custom fields values in WooCommerce order and email notification

Based on "Choosing a date and time after choosing the WooCommerce delivery method" answer code, that displays custom Pickup fields and delivery dates, the following code displays the delivery data of those fields on the order edit pages.
Here is my code:
// View fields in Edit Order Page
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_fields_order_meta', 10, 1 );
function my_custom_fields_order_meta($order){
$delivery_option = $order->get_meta('_delivery_option');
if( $delivery_option == 'date' ) {
$delivery_datetime = $order->get_meta('_delivery_datetime');
echo '<p><strong>'.__('Delivery').':</strong> ' . get_post_meta( $order->id, '_delivery_option', true ) . '</p>';
echo '<p><strong>'.__('Delivery Date').':</strong> ' . get_post_meta( $order->id, '_delivery_datetime', true ) . '</p>';
}
}
Unfortunately, only the delivery date that the customer chooses is displayed correctly, and the options of the radio button "As Soon As Possible" are not shown.
Apparently, I'm doing something wrong.
I would like also to display these fields values on the Thank You page and in the email.
Any help is appreciated.
To display the custom fields values in backend order edit pages (if they are saved in database for the order), use the following:
// View fields in Edit Order Page
add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_custom_fields_value_admin_order', 10, 1 );
function display_custom_fields_value_admin_order( $order ){
// Display the delivery option
if( $delivery_option = $order->get_meta('_delivery_option') )
echo '<p><strong>'.__('Delivery type').':</strong> ' . $delivery_option . '</p>';
// Display the delivery date
if( $delivery_datetime = $order->get_meta('_delivery_datetime') )
echo '<p><strong>'.__('Delivery Date').':</strong> ' . $delivery_datetime . '</p>';
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
The best shorter clean way to display the custom field values everywhere on frontend order pages and on email notifications is to display them in order totals table, just like used payment methods:
// Display the chosen delivery information
add_filter( 'woocommerce_get_order_item_totals', 'chosen_delivery_item_order_totals', 10, 3 );
function chosen_delivery_item_order_totals( $total_rows, $order, $tax_display ) {;
$new_total_rows = [];
// Loop through Order total lines
foreach($total_rows as $key => $total ){
// Get the chosen delivery values
$delivery_option = $order->get_meta('_delivery_option');
$delivery_datetime = $order->get_meta('_delivery_datetime');
// Display delivery information before payment method
if( ! empty($delivery_option) && 'payment_method' === $key ){
$label = empty($delivery_datetime) ? __('Delivery') : __('Delivery Date');
$value = empty($delivery_datetime) ? __('AZAP', $domain) : $delivery_datetime;
// Display 'Delivery method' line
$new_total_rows['chosen_delivery'] = array( 'label' => $label,'value' => $value );
}
$new_total_rows[$key] = $total;
}
return $new_total_rows;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Related thread: Choosing a date and time after choosing the WooCommerce delivery method

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

Add cart meta value for a product after an order request is submitted

I have a multi vendor restaurant WooCommerce site. And one person can order max from one restaurant.On a restaurant when adding product to cart I have added an extra meta-field restaurant_id which is the id of that restaurant. In a cart , all the products have same restaurant_id.
i.e : I have two items in cart having product_id : 12 and 13 but they have same restaurant_id : 366.
I need to add this restaurant_id as meta-field in order-complete or order-process action.because i need to show restaurant name on customers account page.
Or any easy way to do that?
I have tried below code as test
add_action( 'woocommerce_checkout_update_order_meta', 'add_field_to_order' );
function add_field_to_order( $order_id ) {
update_post_meta( $order_id, 'new_field', 'new_value' );
}
But it does not add any meta-field and meta-value to order
— Update —
As I understand now, this custom field already exist and you get this restaurant_id value in cart. So you would like to display that in your view order (thank you and my account pages) and may be on emails…
Here is that code:
//
// ADD HIDDEN IMPUT FIELDS TO THE CHECKOUT
//
add_action( 'woocommerce_after_order_notes', 'checkout_custom_hidden_imput_field' );
function checkout_custom_hidden_imput_field( $checkout ) {
foreach(WC()->cart->get_cart() as $item){
$restaurant_id = $item['restaurant_id'];
break;
}
echo '<div id="custom_checkout_fields" class="custom-hidden-checkout-field">
<input type="hidden" id="restaurant_id" name="restaurant_id" value="'.$restaurant_id.'" />
</div>';
}
//
// SAVE THE ORDER META WITH FIELD VALUE
//
add_action( 'woocommerce_checkout_update_order_meta', 'custom_checkout_field_update_order_meta' );
function custom_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['restaurant_id'] ) ) {
add_post_meta( $order_id, '_restaurant_id', $_POST['restaurant_id'] );
}
}
//
// DISPLAY FIELD VALUE ON THE ORDER EDIT PAGE (NOT IN CUSTOM FIELDS METABOX)
//
add_action( 'woocommerce_admin_order_data_after_billing_address', 'custom_checkout_field_display_admin_order_meta', 10, 1 );
function custom_checkout_field_display_admin_order_meta($order){
$restaurant_id = get_post_meta( $order->id, '_restaurant_id', true );
if ( ! empty( $restaurant_id ) ) {
echo '<p><strong>'. __("Restaurant ID", "woocommerce").':</strong> ' . $restaurant_id . '</p>';
}
}
//
// ADD THE INFORMATION AS META DATA SO THAT IT CAN BE SEEN AS PART OF THE ORDER
//
add_action('woocommerce_add_order_item_meta','custom_add_values_to_order_item_meta', 1, 3 );
function custom_add_values_to_order_item_meta( $item_id, $values, $cart_item_key ) {
$restaurant_id = get_post_meta( $order->id, '_restaurant_id', true );
// lets add the meta data to the order!
wc_add_order_item_meta($item_id, '_restaurant_id', $restaurant_id, true);
}
Code goes in any php file of your active child theme (or theme) or also in any plugin php files.
Code is tested and works.
You could utilize the hook woocommerce_order_status_XXX, where XXX is the order status.
For example:
add_action( 'woocommerce_order_status_completed', 'my_order_status_change_function' );
function my_order_status_change_function( $order_id ) {
$order = new WC_Order($order_id);
//your logic for updating order meta, the meta is stored in wp_postmeta.
update_post_meta( $order_id, 'new_field', 'new_value' );
}
To trigger the function when order status is changed to processing,
change the action name to woocommerce_order_status_processing.

Add custom field data to WooCommerce order

I have a custom field on my WooCommerce single product. It sends to the cart fine, it displays on checkout fine, it shows in the order in the dashboard fine.
What I am now trying to do is set the value as a custom field in the order page so I am able to amend the text when I need to. For some reason when I submit the form this step isn't working.
The code that i use in my functions.phpfile:
// Add the field to the product
add_action('woocommerce_before_add_to_cart_button', 'my_custom_checkout_field');
function my_custom_checkout_field() {
echo '<div id="my_custom_checkout_field"><h3>'.__('My Field').'</h3>';
echo '<label>fill in this field</label> <input type="text" name="my_field_name">';
echo '</div>';
}
// Store custom field
function save_my_custom_checkout_field( $cart_item_data, $product_id ) {
if( isset( $_REQUEST['my_field_name'] ) ) {
$cart_item_data[ 'my_field_name' ] = $_REQUEST['my_field_name'];
/* below statement make sure every add to cart action as unique line item */
$cart_item_data['unique_key'] = md5( microtime().rand() );
}
return $cart_item_data;
}
add_action( 'woocommerce_add_cart_item_data', 'save_my_custom_checkout_field', 10, 2 );
// Render meta on cart and checkout
function render_meta_on_cart_and_checkout( $cart_data, $cart_item = null ) {
$custom_items = array();
/* Woo 2.4.2 updates */
if( !empty( $cart_data ) ) {
$custom_items = $cart_data;
}
if( isset( $cart_item['my_field_name'] ) ) {
$custom_items[] = array( "name" => 'My Field', "value" => $cart_item['my_field_name'] );
}
return $custom_items;
}
add_filter( 'woocommerce_get_item_data', 'render_meta_on_cart_and_checkout', 10, 2 );
// Display as order meta
function my_field_order_meta_handler( $item_id, $values, $cart_item_key ) {
if( isset( $values['my_field_name'] ) ) {
wc_add_order_item_meta( $item_id, "my_field_name", $values['my_field_name'] );
}
}
add_action( 'woocommerce_add_order_item_meta', 'my_field_order_meta_handler', 1, 3 );
/** THIS IS WHERE I'M STUCK **/
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
global $woocommerce;
// Check if set, if its not set add an error. This one is only requite for companies
if ($_POST['billing_company'])
if (!$_POST['my_field_name'])
$woocommerce->add_error( __('Please enter your XXX.') );
}
// Update the user meta with field value
add_action('woocommerce_checkout_update_user_meta', 'my_custom_checkout_field_update_user_meta');
function my_custom_checkout_field_update_user_meta( $user_id ) {
if ($user_id && $_POST['my_field_name']) update_user_meta( $user_id, 'my_field_name', esc_attr($_POST['my_field_name']) );
}
// Update the order meta with field value
add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta');
function my_custom_checkout_field_update_order_meta( $order_id ) {
if ($_POST['my_field_name']) update_post_meta( $order_id, 'My Field', esc_attr($_POST['my_field_name']));
}
Screenshot of what currently happens:
What I would like to happen:
Any help would be greatly appreciated.
Updated: compatibility with Woocommerce version 3+
You have missing the function to display this custom field value on the order edit page:
/**
* Display field value on the order edit page
*/
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta( $order ){
$order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;
echo '<p><strong>'.__('My Field Name').':</strong> ' . get_post_meta( $order_id, 'my_field_name', true ) . '</p>';
}
On the reference link below, you have all original wooThemes functional working code snippets. It's an excellent fully functional tutorial.
Reference: [Customizing checkout fields using actions and filters][1]
Edit: Get a custom label displayed with your custom field value in Order item meta
To get a custom label like "MY field name" with your custom field value (in order items meta) instead of a slug like my_field_name, refer to this treads:
Saving a product custom field and displaying it in cart page
Displaying product custom fields values in the order once processed
Adding user custom field value to order items details
I don't know if this still is relevant, I have tried to do this with code, unfortunately, I got stuck in the way. I have tried this woocommerce checkout field editor plugin, which did well to add custom field data to woocomemrce order.

Categories