Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Display custom fields in woocommerce order completed email WordPress

  • SOLVED

Hello,

I am using a code in my woocommerce completed email to show an order custom field from plugin Aftership. The code is now giving me problems and not showing the information in order completed email.

add_action( 'woocommerce_email_before_order_table', 'wc_email_before_order_table', 20 );
function wc_email_before_order_table( $order ) {
if ( 'completed' == $order->status ) {
$provider = get_post_meta( $order->get_id(),'_aftership_tracking_provider',true);
$number = get_post_meta( $order->get_id(),'_aftership_tracking_number',true);
echo sprintf( __( '<p><a href="https://track.brightledshoes.com/%s">Click Here To Track Your Package</a></p>', 'woocommerce' ), $number );
echo sprintf( __( '<p><font color="white">Tracking Number: %s</font></p>', 'woocommerce' ), $number );
}}


I have attached image with example of email that is correct and displaying information and current email that is not showing information. If I send woocommerce invoice email then customer information displays correctly.

I think the custom fields for the aftership plugin which are _aftership_tracking_number and _aftership_tracking_provider are not registering when order is marked as completed. I use wp all import to update order with custom fields and mark as 'completed'.

Answers (3)

2020-04-21

Rempty answers:

I think importing not trigger the proper email.
can you test changing a bit the function


add_action( 'woocommerce_email_before_order_table', 'wc_email_before_order_table', 20 );
function wc_email_before_order_table( $order ) {
$provider = get_post_meta( $order->get_id(),'_aftership_tracking_provider',true);
$number = get_post_meta( $order->get_id(),'_aftership_tracking_number',true);
if ( !empty($provider ) && !empty($number) ) {
echo sprintf( __( '<p><a href="https://track.brightledshoes.com/%s">Click Here To Track Your Package</a></p>', 'woocommerce' ), $number );
echo sprintf( __( '<p><font color="white">Tracking Number: %s</font></p>', 'woocommerce' ), $number );
}
}


Instead of check order complete it will check if the 2 tracking meta field are not empty.


George Sprouse comments:

Hi Rempty,

I tried the code and problem still is there. When I do import the correct email is triggered but information still missing.


Rempty comments:

try

add_action( 'woocommerce_email_before_order_table', 'wc_email_before_order_table', 20,4 );
function wc_email_before_order_table( $order, $sent_to_admin, $plain_text, $email ) {
$provider = get_post_meta( $order->get_id(),'_aftership_tracking_provider',true);
$number = get_post_meta( $order->get_id(),'_aftership_tracking_number',true);
if ( !empty($provider ) || !empty($number) ) {
echo sprintf( __( '<p><a href="https://track.brightledshoes.com/%s">Click Here To Track Your Package</a></p>', 'woocommerce' ), $number );
echo sprintf( __( '<p><font color="white">Tracking Number: %s</font></p>', 'woocommerce' ), $number );
}
}


George Sprouse comments:

Hi Rempty still not working :(


Rempty comments:

can you check your php error.log?

try this one to test if is an error in the printf

add_action( 'woocommerce_email_before_order_table', 'wc_email_before_order_table', 20,4 );
function wc_email_before_order_table( $order, $sent_to_admin, $plain_text, $email ) {
$provider = get_post_meta( $order->get_id(),'_aftership_tracking_provider',true);
$number = get_post_meta( $order->get_id(),'_aftership_tracking_number',true);
if ( !empty($provider ) || !empty($number) ) {
echo 'Provider and number must be here';
}
}

2020-04-21

Mohamed Ahmed answers:

Hello George,
Hope you are doing well and safe!

Could you try this code and tell me the result now


add_action( 'woocommerce_email_before_order_table', 'wc_email_before_order_table', 20 );
function wc_email_before_order_table( $order ) {
if ( 'completed' == $order->status ) {
$provider = get_post_meta( $order->get_id(),'_aftership_tracking_provider',true);
$number = get_post_meta( $order->get_id(),'_aftership_tracking_number',true);

if($provider && $number){
/* If Tracking Provider and Number tracking are provided then show the info*/

echo sprintf( __( '<p><a href="https://track.brightledshoes.com/%s">Click Here To Track Your Package</a></p>', 'woocommerce' ), $number );
echo sprintf( __( '<p><font color="white">Tracking Number: %s</font></p>', 'woocommerce' ), $number );
}
elseif (!$provider) {
/* If Tracking Provider is missing */
echo ' No Tracking Provider';
}
elseif (!$number) {
/* If Tracking Number is missing */
echo ' No Tracking Number';
}

} else {
/* If $order->status is not a completed status */
echo 'Order is not completed';
}

}


George Sprouse comments:

Hi Mohamed

Thanks for your response. I tried the code but still not working. Attached image of email


Mohamed Ahmed comments:

I had wrote this code for checking and I confirmed and know the issue
The code mean there isn't Tracking Provider !!
If you didn't do it, Please follow this tutorial to do it
https://docs.woocommerce.com/document/shipment-tracking/

2020-04-24

Arnav Joy answers:

Hello George,
is your problem solved?


George Sprouse comments:

Hi Arnav hope you are well..the problem has not been solved


Arnav Joy comments:

yes I am good,is it possible you can send me access to the site?
you can use my email.