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
Related
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.
In Woocommerce I am using WooCommerce Print Invoices & Packing Lists plugin and I am trying to display a custom order meta data value which is a delivery date in an invoice.
This is my code in my theme's functions.php file:
function sv_wc_pip_add_order_delivery_shipping( $order_id, $order ) {
//$order_id = 25775; (this method is working but is static and not dynamic)
$order_id = $order->get_id();
$invoice_web_delivery_date = get_post_meta($order_id,'jckwds_date',true);
echo $invoice_web_delivery_date ;
}
add_filter( 'wc_pip_after_customer_addresses', 'sv_wc_pip_add_order_delivery_shipping', 10, 3 );
But the Order_id is not working and not returning the order id for each woocommerce order.
How can I modify this code to get the correct ID for each order?
The arguments for this hooked function are quite different. Try the following instead:
add_action( 'wc_pip_after_customer_addresses', 'action_after_customer_addresses', 10, 4 );
function action_after_customer_addresses( $type, $action, $document, $order ) {
if( $ddate = $order->get_meta( 'jckwds_date' ) )
echo '<p>'.__("Delivery date") . ': ' . $ddate . '</p>';
}
}
Code goes in function.php file of your active child theme (or active theme). It should works.
Official documentation for wc_pip_after_customer_addresses action hook:
I have a custom function that checks if a checkbox is checked and if so, it adds 'with vat relief' next to the price. If it isn't checked, it adds 'inc vat' next to the price. That works fine and my code is:
add_filter( 'woocommerce_get_price_html', 'conditional_price_suffix', 20, 2 );
function conditional_price_suffix( $price, $product ) {
$isTaxRelefe = get_post_meta($product->id, 'disability_exemption', true);
if ($isTaxRelefe == 'yes')
$price .= ' ' . __('with vat relief');
else $price .= ' ' . __('inc vat');
return $price;
}
What I need to do now is add another function targeting the checkout page that says if the checkbox is checked show some text underneath the product title but I'm struggling. My initial thought was to edit the /checkout/review-order so I added an if else statement to output something next to the product title is. I added:
$isTaxRelefe = get_post_meta($product->id, 'disability_exemption', true);
if ($isTaxRelefe == 'yes') {
$content .= 'VAT RELIEF AVAILABLE';
}
but this does nothing, I have tried various variations, changing to echo statements etc. but no luck. I'm sure I am just writing this incorrectly. Can anyone advise? What I'm not very up on are WordPress functions as in if I could write one to target the checkout page only, Im not sure how it determines where to output your. an if else statement seemed like the obvious choice but not having any luck.
Your code is a bit outdated and you should use $product->get_id() since Woocommerce 3 in your first function instead of $product->id in the get_post_meta() function.
You can also use instead the WC_Data method get_meta() from the product object directly.
Below is your revisited code with the additional hooked function that will display conditionally "VAT RELIEF AVAILABLE" text under the product title in checkout page: (without overriding the template review-order.php)
add_filter( 'woocommerce_get_price_html', 'conditional_price_suffix', 20, 2 );
function conditional_price_suffix( $price, $product ) {
if ( $product->get_meta('disability_exemption') === 'yes')
$price .= ' ' . __('with vat relief');
else
$price .= ' ' . __('inc vat');
return $price;
}
add_filter( 'woocommerce_checkout_cart_item_quantity', 'custom_text_below_checkout_product_title', 20, 3 );
function custom_text_below_checkout_product_title( $quantity_html, $cart_item, $cart_item_key ){
if ( $cart_item['data']->get_meta('disability_exemption') === 'yes' )
$quantity_html .= '<br>' . __('VAT RELIEF AVAILABLE');
return $quantity_html;
}
Code goes in function.php file of your active child theme (active theme). Tested and works.
I'm trying to find a way to place custom text before availability info (out of stock, in stock etc.) on woocommerce single product page.
I am using something like that:
add_filter( 'woocommerce_get_availability', 'change_product_availability_display' );
add_filter( 'unknown filter', 'change_product_availability_display' );
function change_product_availability_display( $availability ) {
// Additional text
$text = __('Availability:');
// returning the text before the availability
return $text . ' ' . $availability;
}
Its's based on: Add a custom text before the price display in WooCommerce.
In case of price filter named in my code "unknown filter" was woocommerce_cart_item_price. I've looked for this kind of filter for availability/stock item, but can't find it.
Maybe someone could review this code and help me to find this "unknown_filter" or have other idea how I can put custom text before availability info?
I have revisited your code as your are trying to merge a string with an array. Also I have added a 2nd hooked function that will allow to display your "labeled" availability to items in cart and checkout.
The code:
add_filter( 'woocommerce_get_availability', 'add_label_to_availability_display' );
function add_label_to_availability_display( $availability ) {
if( is_product() || is_cart() || is_checkout() ){
$label = __( 'Availability', 'woocommerce' ) . ': ';
$availability['availability'] = $label . $availability['availability'];
}
return $availability;
}
add_filter( 'woocommerce_cart_item_name', 'add_availability_below_cart_item_name', 10, 3);
function add_availability_below_cart_item_name( $item_name, $cart_item, $cart_item_key ) {
$availability = $cart_item['data']->get_availability();
return $item_name . '<br>' . $availability['availability'];
}
Code goes in function.php file of your active child theme (active theme).
Tested and works.
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.