Adding a custom field value to Woocommerce order email template - php

In Woocommerce with Advanced Custom Fields plugin, we have added a custom field to products and this field value is specific to each product.
Now I am trying to add this custom field value to our Woocommerce order confirmation emails.
I have tried the following code with no success:
<?php
if ( version_compare( WC_VERSION, '3.0', '<' ) ) {
$order_id = int_val( $order->id ); // Older than 3.0
} else {
$order_id = int_val( $order->get_id() ); // 3.0+
}
$inst1 = get_field(‘how1’, $order_id );
if( $inst1 ){
echo '<p>' . $inst1 . '</p>';
}
?> with Advanced Custom Fields plugin

As your custom field is specific to "product" post type (but NOT to "order" post type) you need to get first the order items to get the product ID that you should use with ACF get_field() function this way:
<?php
foreach ( $order->get_items() as $item ) {
// Get the product ID
if ( version_compare( WC_VERSION, '3.0', '<' ) ) {
$product_id = $item['product_id']; // Older than 3.0
} else {
$product_id = $item->get_product_id(); // 3.0+
}
$inst1 = get_field( 'how1', $product_id );
if( $inst1 ){
echo '<p>' . $inst1 . '</p>';
}
}
<?
The custom field value will be displayed for each item in the order, as an order can have many items in it.
References:
How to get WooCommerce order details
Get Order items and WC_Order_Item_Product in Woocommerce 3

Related

Show product meta in order items table on WooCommerce admin order details page

I am using the following code to show custom product meta in the order items table on the WooCommerce admin order details page:
add_action('woocommerce_admin_order_item_headers', 'tempiconsegna_admin_order_item_headers');
function tempiconsegna_admin_order_item_headers() {
$column_name = 'Tempi Consegna';
echo '<th>' . $column_name . '</th>';
}
add_action('woocommerce_admin_order_item_values', 'tempiconsegna_admin_order_item_values', 10, 3);
function tempiconsegna_admin_order_item_values($_product, $item, $item_id = null) {
$value = get_post_meta($_product->post->ID, 'prefix-tempiconsegna', 1);
echo '<td>' . $value . '</td>';
}
It displays "prefix-tempiconsegna" which are custom metas like:
Available in 3 days
Available now
etc..
My problem is that if I change the availability in the product, it changes also in previous orders.
How do i make this displaying the value at the moment of the order without changing when I update the availability of the product?
Your current code contains 2 errors:
Attempt to read property "post" on null
Attempt to read property "ID" on null
To answer your question: that's because you're using get_post_meta() and the productID, so if you adjust the data for the product it will also change the data where it is displayed, in your case the current and previous orders.
To prevent this, you have to add the data per order line item, this can be done via:
function action_woocommerce_checkout_create_order_line_item( $item, $cart_item_key, $values, $order ) {
// The WC_Product instance Object
$product = $item->get_product();
// Get value
$value = $product->get_meta( 'prefix-tempiconsegna' );
// NOT empty
if ( ! empty ( $value ) ) {
$item->update_meta_data( 'prefix-tempiconsegna', $value );
}
}
add_action( 'woocommerce_checkout_create_order_line_item', 'action_woocommerce_checkout_create_order_line_item', 10, 4 );
Then to display this in WooCommerce admin order details page, use:
// Add header
function action_woocommerce_admin_order_item_headers( $order ) {
// Set the column name
$column_name = __( 'Tempi Consegna', 'woocommerce' );
// Display the column name
echo '<th class="my-class">' . $column_name . '</th>';
}
add_action( 'woocommerce_admin_order_item_headers', 'action_woocommerce_admin_order_item_headers', 10, 1 );
//Add content
function action_woocommerce_admin_order_item_values( $product, $item, $item_id ) {
// Only for "line_item" items type, to avoid errors
if ( ! $item->is_type('line_item') ) return;
// Get value
$value = $item->get_meta( 'prefix-tempiconsegna' );
// NOT empty
if ( ! empty ( $value ) ) {
echo '<td>' . $value . '</td>';
} else {
echo '<td>Meta not found!</td>';
}
}
add_action( 'woocommerce_admin_order_item_values', 'action_woocommerce_admin_order_item_values', 10, 3 );

ACF custom fields in WooCommerce E-Mail template

I'm wanting to display custom ACF fields in a product within the order complete e-mail, I have this hook which works great for none-variable products:
add_filter( 'woocommerce_order_item_name', 'custom_order_item_name', 10, 2 );
function custom_order_item_name( $item_name, $item ) {
// Targeting email notifications only
if( is_wc_endpoint_url() )
return $item_name;
// Get the WC_Product object (from order item)
$product = $item->get_product();
if( $date = get_field('date', $product->get_id()) ) {
$item_name .= '<br /><strong>' . __( 'Date', 'woocommerce' ) . ': </strong>' . $date;
}
if( $location = get_field('location', $product->get_id()) ) {
$item_name .= '<br /><strong>' . __( 'Location', 'woocommerce' ) . ': </strong>' . $location;
}
return $item_name;
}
However, while it displays my custom fields (date and location) fine for simple products within the e-mail, it does not for variable products.
I can't seem to understand why?
I found the solution.
When it is a simple product, the product ID is the post ID. However when it is a variable product, they then use a variable product ID, not the post ID. Which means the ACF fields are not looking at the post ID of the product, so won't display.
To fix this for variable products you must get the parent ID from the array:
$parent_id=$product->get_parent_id();
// If it is a variable product, get the parent ID
if($parent_id){
$product_id = $parent_id;
// else, it is a simple product, get the product ID
}else{
$product_id = $product->get_id();
}
Full code is:
// Display Items Shipping ACF custom field value in email notification
add_filter( 'woocommerce_order_item_name', 'custom_order_item_name', 10, 2 );
function custom_order_item_name( $item_name, $item ) {
// Targeting email notifications only
if( is_wc_endpoint_url() )
return $item_name;
// Get the WC_Product object (from order item)
$product = $item->get_product();
$parent_id=$product->get_parent_id();
// If it is a variable product, get the parent ID
if($parent_id){
$product_id = $parent_id;
// else, it is a simple product, get the product ID
}else{
$product_id = $product->get_id();
}
if( $date = get_field('date', $product_id) ) {
$item_name .= '<br /><strong>' . __( 'Date', 'woocommerce' ) . ': </strong>' . $date;
}
if( $location = get_field('location', $product_id) ) {
$item_name .= '<br /><strong>' . __( 'Location', 'woocommerce' ) . ': </strong>' . $location;
}
return $item_name;
}

Display specific product attribute in WooCommerce email notifications

I can't manage to add a product attribute to a WooCommerce new order email. I have added the snippet below to email-order-items.php (after // SKU.. part), but nothing happens. Even the titel 'Location:' isn't visible. Any thoughts on this?
// Attribute
if ( $item_meta->meta ) {echo '<br/><small>Location: ' . nl2br( $product->get_attribute( 'location' ) ) . '</small>';}
Updated - Instead of overriding Woocommerce templates, always try first to use available hooks like:
add_action( 'woocommerce_order_item_meta_start', 'add_download_links_to_thank_you_page', 10, 3 );
function add_download_links_to_thank_you_page( $item_id, $item, $order ) {
// Set below your product attribute taxonomy (always starts with "pa_")
$taxonomy = 'pa_location';
// On email notifications
if ( ! is_wc_endpoint_url() && $item->is_type('line_item') ) {
$product = $item->get_product();
$label_name = get_taxonomy( $taxonomy )->labels->singular_name;
if ( $term_names = $product->get_attribute( $taxonomy ) ) {
echo '<br/><small>' . $label_name . ': ' . nl2br( $term_names ) . '</small>';
}
}
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
You can also use woocommerce_order_item_meta_end hook instead.

Advanced Custom Fields in WooCommerce Email templates

I am using Advanced Custom Fields (ACF) plugin in WooCommerce and I have set a custom field named "tracking-no".
How can I display the value of this custom field in the Woocommerce template emails/customer-completed-order.php?
I am using this code:
<?php
if(get_field('tracking-no'))
{
echo '<p>' . get_field('tracking-no') . '</p>';
}
?>
But I doesn't get anything.
Thanks
In your WooCommerce template you should get first the order ID as argument in get_field():
<?php
// Get the $order ID (WooCommerce version compatibility)
if ( version_compare( WC_VERSION, '3.0', '<' ) ) {
$order_id = int_val( $order->id ); // Older than 3.0
} else {
$order_id = int_val( $order->get_id() ); // 3.0+
}
$tracking_num = get_field('tracking-no', $order_id );
if( $tracking_num ){
echo '<p>' . $tracking_num . '</p>';
}
?>
You can also use instead any email notification hook that you can find on this template, this way:
add_action( 'woocommerce_email_order_details', 'my_custom_field_in_completed_notification', 10, 4 );
function my_custom_field_in_completed_notification( $order, $sent_to_admin, $plain_text, $email ){
// Get the $order ID (WooCommerce version compatibility)
if ( version_compare( WC_VERSION, '3.0', '<' ) ) {
$order_id = int_val( $order->id ); // Older than 3.0
} else {
$order_id = int_val( $order->get_id() ); // 3.0+
}
$tracking_num = get_field('tracking-no', $order_id );
if( $tracking_num ){
echo '<p>' . $tracking_num . '</p>';
}
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
In this case we don't override WooCommerce templates. You can also use that 2 similar hooks:
woocommerce_email_order_meta
woocommerce_email_customer_details

Getting order data after successful checkout hook

In WooCommerce, I would like to send a request to an API once the customer has successfully checked out. Its basically a website where the client is selling online courses (Like udemy).
When the customer checks out, I would like to send an API request and enroll the user for that particular course. I have tried several WooCommerce hooks but none worked for me.
This is the code that I'm using:
add_action('woocommerce_checkout_order_processed', 'enroll_student', 10, 1);
function enroll_student($order_id)
{
echo $order_id;
echo "Hooked";
}
I am writing this code for a plugin and to make it easier, I am currently using Cash on Delivery method.
Can anyone point me out where I am going wrong because when I checkout I cant see the message "hooked" that I am printing nor the $order_id?
It takes me to the success page and doesn't show these two things that I am printing.
Update 2 Only For Woocommerce 3+ (added restriction to execute the code only once)
add_action('woocommerce_thankyou', 'enroll_student', 10, 1);
function enroll_student( $order_id ) {
if ( ! $order_id )
return;
// Allow code execution only once
if( ! get_post_meta( $order_id, '_thankyou_action_done', true ) ) {
// Get an instance of the WC_Order object
$order = wc_get_order( $order_id );
// Get the order key
$order_key = $order->get_order_key();
// Get the order number
$order_key = $order->get_order_number();
if($order->is_paid())
$paid = __('yes');
else
$paid = __('no');
// Loop through order items
foreach ( $order->get_items() as $item_id => $item ) {
// Get the product object
$product = $item->get_product();
// Get the product Id
$product_id = $product->get_id();
// Get the product name
$product_id = $item->get_name();
}
// Output some data
echo '<p>Order ID: '. $order_id . ' — Order Status: ' . $order->get_status() . ' — Order is paid: ' . $paid . '</p>';
// Flag the action as done (to avoid repetitions on reload for example)
$order->update_meta_data( '_thankyou_action_done', true );
$order->save();
}
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Related thread:
Get Order items and WC_Order_Item_Product in WooCommerce 3
How to get WooCommerce order details
The code is tested and works.
Updated (to get the product Id from Orders items as asked in your comment)
May be you could use woocommerce_thankyou hook instead, that will display on order-received page your echoed code, this way:
add_action('woocommerce_thankyou', 'enroll_student', 10, 1);
function enroll_student( $order_id ) {
if ( ! $order_id )
return;
// Getting an instance of the order object
$order = wc_get_order( $order_id );
if($order->is_paid())
$paid = 'yes';
else
$paid = 'no';
// iterating through each order items (getting product ID and the product object)
// (work for simple and variable products)
foreach ( $order->get_items() as $item_id => $item ) {
if( $item['variation_id'] > 0 ){
$product_id = $item['variation_id']; // variable product
} else {
$product_id = $item['product_id']; // simple product
}
// Get the product object
$product = wc_get_product( $product_id );
}
// Ouptput some data
echo '<p>Order ID: '. $order_id . ' — Order Status: ' . $order->get_status() . ' — Order is paid: ' . $paid . '</p>';
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
The code is tested and works.
Then you can use all class WC_Abstract_Order methods on the $order object.
Related:
How to get WooCommerce order details
Get Order items and WC_Order_Item_Product in WooCommerce 3
How to get Customer details from Order in WooCommerce?
Rather than 'woocommerce_thankyou' hook, 'woocommerce_checkout_order_processed' hook is the relevant hook. 'woocommerce_checkout_order_processed' hook will be called only once and you will not need to add meta for each product and make additional calls to keep check for code to run only once. As, 'woocommerce_thankyou' can be called multiple times that is each time thankyou page loads.
Replace add_action('woocommerce_thankyou', 'enroll_student', 10, 1);
with
add_action('woocommerce_checkout_order_processed', 'enroll_student', 10, 1);
and remove meta code and checks. Updated code is
add_action('woocommerce_checkout_order_processed', 'enroll_student', 10, 1);
function enroll_student( $order_id ) {
// Getting an instance of the order object
$order = wc_get_order( $order_id );
if($order->is_paid())
$paid = 'yes';
else
$paid = 'no';
// iterating through each order items (getting product ID and the product object)
// (work for simple and variable products)
foreach ( $order->get_items() as $item_id => $item ) {
if( $item['variation_id'] > 0 ){
$product_id = $item['variation_id']; // variable product
} else {
$product_id = $item['product_id']; // simple product
}
// Get the product object
$product = wc_get_product( $product_id );
}
// Ouptput some data
echo '<p>Order ID: '. $order_id . ' — Order Status: ' . $order->get_status() . ' — Order is paid: ' . $paid . '</p>';
}
you can get the order items of an order by
// Getting an instance of the order object
$order = new WC_Order( $order_id );
$items = $order->get_items();
//Loop through them, you can get all the relevant data:
foreach ( $items as $item ) {
$product_name = $item['name'];
$product_id = $item['product_id'];
$product_variation_id = $item['variation_id'];
}

Categories