Set a Tracking Number link on customer email notifications in Woocommerce 3












1














I've managed to get this together and now I'm trying to make the number that is being entered into the custom field (saved to the order) to be linked in the order email.



Here's the code:



// create the metabox
add_action( 'add_meta_boxes', 'bdev_add_postnord_meta_box' );
if ( ! function_exists( 'bdev_add_postnord_meta_box' ) )
{
function bdev_add_postnord_meta_box()
{
add_meta_box( 'postnord_field', __('PostNord Parcel ID','woocommerce'), 'bdev_add_postnord_for_tracking', 'shop_order', 'side', 'core' );
}
}

if ( ! function_exists( 'bdev_add_postnord_for_tracking' ) )
{
function bdev_add_postnord_for_tracking()
{
global $post;

$postnord_field_data = get_post_meta( $post->ID, '_postnord_field_data', true ) ? get_post_meta( $post->ID, '_postnord_field_data', true ) : '';

echo '<input type="hidden" name="postnord_meta_field_nonce" value="' . wp_create_nonce() . '">
<p style="border-bottom:solid 1px #eee;padding-bottom:13px;">
<input type="text" style="width:250px;";" name="postnord_data_name" placeholder="' . $postnord_field_data . '" value="' . $postnord_field_data . '"></p>';

}
}

// save input from metabox
add_action( 'save_post', 'bdev_save_postnord_data_to_order', 10, 1 );
if ( ! function_exists( 'bdev_save_postnord_data_to_order' ) )
{

function bdev_save_postnord_data_to_order( $post_id ) {

if ( ! isset( $_POST[ 'postnord_meta_field_nonce' ] ) ) {
return $post_id;
}
$nonce = $_REQUEST[ 'postnord_meta_field_nonce' ];

if ( ! wp_verify_nonce( $nonce ) ) {
return $post_id;
}

if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}

if ( 'page' == $_POST[ 'post_type' ] ) {

if ( ! current_user_can( 'edit_page', $post_id ) ) {
return $post_id;
}
} else {

if ( ! current_user_can( 'edit_post', $post_id ) ) {
return $post_id;
}
}
update_post_meta( $post_id, '_postnord_field_data', $_POST[ 'postnord_data_name' ] );
}
}


// print tracking info under shipping address
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'postnord_custom_field_display_admin_order_meta', 10, 1 );
function postnord_custom_field_display_admin_order_meta($order){
$postnord_id_field = get_post_meta( $order->id, '_postnord_field_data', true );
if ( ! empty( $postnord_id_field ) ) {
echo '<p><strong>'. __("PostNord Tracking ID", "woocommerce").':</strong> ' . get_post_meta( $order->id, '_postnord_field_data', true ) . '</p>';
}
}


// add information to order email
add_action( 'woocommerce_email_after_order_table', 'add_postnord_tracking_to_customer_complete_order_email', 20, 2 );
function add_postnord_tracking_to_customer_complete_order_email( $order, $sent_to_admin ) {

if ( ! $sent_to_admin ) {
echo '<h2>Track Your Order</h2>';
echo '<p><strong>'. __("PostNord Tracking ID", "woocommerce").':</strong> ' . get_post_meta( $order->id, '_postnord_field_data', true ) . '</p>';
echo '<p>You can track your parcel on the <a href="https://www.postnord.se/en/online-tools/tools/track/track-and-trace" target="_blank" rel="">PostNord website</a> or directly from <a href="#" target="_blank" rel="">our site</a>.<br><br>';
}
}


The number is saved to the order and I would like to link it so that the customer can click it. When clicked, it should open a URL followed by the tracking number, like this: https://example.com/tracking-page/trackingnr



Any help is appreciated.










share|improve this question
























  • Updated the save_post hooked function for a better one made for orders.
    – LoicTheAztec
    Sep 16 '18 at 22:17
















1














I've managed to get this together and now I'm trying to make the number that is being entered into the custom field (saved to the order) to be linked in the order email.



Here's the code:



// create the metabox
add_action( 'add_meta_boxes', 'bdev_add_postnord_meta_box' );
if ( ! function_exists( 'bdev_add_postnord_meta_box' ) )
{
function bdev_add_postnord_meta_box()
{
add_meta_box( 'postnord_field', __('PostNord Parcel ID','woocommerce'), 'bdev_add_postnord_for_tracking', 'shop_order', 'side', 'core' );
}
}

if ( ! function_exists( 'bdev_add_postnord_for_tracking' ) )
{
function bdev_add_postnord_for_tracking()
{
global $post;

$postnord_field_data = get_post_meta( $post->ID, '_postnord_field_data', true ) ? get_post_meta( $post->ID, '_postnord_field_data', true ) : '';

echo '<input type="hidden" name="postnord_meta_field_nonce" value="' . wp_create_nonce() . '">
<p style="border-bottom:solid 1px #eee;padding-bottom:13px;">
<input type="text" style="width:250px;";" name="postnord_data_name" placeholder="' . $postnord_field_data . '" value="' . $postnord_field_data . '"></p>';

}
}

// save input from metabox
add_action( 'save_post', 'bdev_save_postnord_data_to_order', 10, 1 );
if ( ! function_exists( 'bdev_save_postnord_data_to_order' ) )
{

function bdev_save_postnord_data_to_order( $post_id ) {

if ( ! isset( $_POST[ 'postnord_meta_field_nonce' ] ) ) {
return $post_id;
}
$nonce = $_REQUEST[ 'postnord_meta_field_nonce' ];

if ( ! wp_verify_nonce( $nonce ) ) {
return $post_id;
}

if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}

if ( 'page' == $_POST[ 'post_type' ] ) {

if ( ! current_user_can( 'edit_page', $post_id ) ) {
return $post_id;
}
} else {

if ( ! current_user_can( 'edit_post', $post_id ) ) {
return $post_id;
}
}
update_post_meta( $post_id, '_postnord_field_data', $_POST[ 'postnord_data_name' ] );
}
}


// print tracking info under shipping address
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'postnord_custom_field_display_admin_order_meta', 10, 1 );
function postnord_custom_field_display_admin_order_meta($order){
$postnord_id_field = get_post_meta( $order->id, '_postnord_field_data', true );
if ( ! empty( $postnord_id_field ) ) {
echo '<p><strong>'. __("PostNord Tracking ID", "woocommerce").':</strong> ' . get_post_meta( $order->id, '_postnord_field_data', true ) . '</p>';
}
}


// add information to order email
add_action( 'woocommerce_email_after_order_table', 'add_postnord_tracking_to_customer_complete_order_email', 20, 2 );
function add_postnord_tracking_to_customer_complete_order_email( $order, $sent_to_admin ) {

if ( ! $sent_to_admin ) {
echo '<h2>Track Your Order</h2>';
echo '<p><strong>'. __("PostNord Tracking ID", "woocommerce").':</strong> ' . get_post_meta( $order->id, '_postnord_field_data', true ) . '</p>';
echo '<p>You can track your parcel on the <a href="https://www.postnord.se/en/online-tools/tools/track/track-and-trace" target="_blank" rel="">PostNord website</a> or directly from <a href="#" target="_blank" rel="">our site</a>.<br><br>';
}
}


The number is saved to the order and I would like to link it so that the customer can click it. When clicked, it should open a URL followed by the tracking number, like this: https://example.com/tracking-page/trackingnr



Any help is appreciated.










share|improve this question
























  • Updated the save_post hooked function for a better one made for orders.
    – LoicTheAztec
    Sep 16 '18 at 22:17














1












1








1


1





I've managed to get this together and now I'm trying to make the number that is being entered into the custom field (saved to the order) to be linked in the order email.



Here's the code:



// create the metabox
add_action( 'add_meta_boxes', 'bdev_add_postnord_meta_box' );
if ( ! function_exists( 'bdev_add_postnord_meta_box' ) )
{
function bdev_add_postnord_meta_box()
{
add_meta_box( 'postnord_field', __('PostNord Parcel ID','woocommerce'), 'bdev_add_postnord_for_tracking', 'shop_order', 'side', 'core' );
}
}

if ( ! function_exists( 'bdev_add_postnord_for_tracking' ) )
{
function bdev_add_postnord_for_tracking()
{
global $post;

$postnord_field_data = get_post_meta( $post->ID, '_postnord_field_data', true ) ? get_post_meta( $post->ID, '_postnord_field_data', true ) : '';

echo '<input type="hidden" name="postnord_meta_field_nonce" value="' . wp_create_nonce() . '">
<p style="border-bottom:solid 1px #eee;padding-bottom:13px;">
<input type="text" style="width:250px;";" name="postnord_data_name" placeholder="' . $postnord_field_data . '" value="' . $postnord_field_data . '"></p>';

}
}

// save input from metabox
add_action( 'save_post', 'bdev_save_postnord_data_to_order', 10, 1 );
if ( ! function_exists( 'bdev_save_postnord_data_to_order' ) )
{

function bdev_save_postnord_data_to_order( $post_id ) {

if ( ! isset( $_POST[ 'postnord_meta_field_nonce' ] ) ) {
return $post_id;
}
$nonce = $_REQUEST[ 'postnord_meta_field_nonce' ];

if ( ! wp_verify_nonce( $nonce ) ) {
return $post_id;
}

if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}

if ( 'page' == $_POST[ 'post_type' ] ) {

if ( ! current_user_can( 'edit_page', $post_id ) ) {
return $post_id;
}
} else {

if ( ! current_user_can( 'edit_post', $post_id ) ) {
return $post_id;
}
}
update_post_meta( $post_id, '_postnord_field_data', $_POST[ 'postnord_data_name' ] );
}
}


// print tracking info under shipping address
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'postnord_custom_field_display_admin_order_meta', 10, 1 );
function postnord_custom_field_display_admin_order_meta($order){
$postnord_id_field = get_post_meta( $order->id, '_postnord_field_data', true );
if ( ! empty( $postnord_id_field ) ) {
echo '<p><strong>'. __("PostNord Tracking ID", "woocommerce").':</strong> ' . get_post_meta( $order->id, '_postnord_field_data', true ) . '</p>';
}
}


// add information to order email
add_action( 'woocommerce_email_after_order_table', 'add_postnord_tracking_to_customer_complete_order_email', 20, 2 );
function add_postnord_tracking_to_customer_complete_order_email( $order, $sent_to_admin ) {

if ( ! $sent_to_admin ) {
echo '<h2>Track Your Order</h2>';
echo '<p><strong>'. __("PostNord Tracking ID", "woocommerce").':</strong> ' . get_post_meta( $order->id, '_postnord_field_data', true ) . '</p>';
echo '<p>You can track your parcel on the <a href="https://www.postnord.se/en/online-tools/tools/track/track-and-trace" target="_blank" rel="">PostNord website</a> or directly from <a href="#" target="_blank" rel="">our site</a>.<br><br>';
}
}


The number is saved to the order and I would like to link it so that the customer can click it. When clicked, it should open a URL followed by the tracking number, like this: https://example.com/tracking-page/trackingnr



Any help is appreciated.










share|improve this question















I've managed to get this together and now I'm trying to make the number that is being entered into the custom field (saved to the order) to be linked in the order email.



Here's the code:



// create the metabox
add_action( 'add_meta_boxes', 'bdev_add_postnord_meta_box' );
if ( ! function_exists( 'bdev_add_postnord_meta_box' ) )
{
function bdev_add_postnord_meta_box()
{
add_meta_box( 'postnord_field', __('PostNord Parcel ID','woocommerce'), 'bdev_add_postnord_for_tracking', 'shop_order', 'side', 'core' );
}
}

if ( ! function_exists( 'bdev_add_postnord_for_tracking' ) )
{
function bdev_add_postnord_for_tracking()
{
global $post;

$postnord_field_data = get_post_meta( $post->ID, '_postnord_field_data', true ) ? get_post_meta( $post->ID, '_postnord_field_data', true ) : '';

echo '<input type="hidden" name="postnord_meta_field_nonce" value="' . wp_create_nonce() . '">
<p style="border-bottom:solid 1px #eee;padding-bottom:13px;">
<input type="text" style="width:250px;";" name="postnord_data_name" placeholder="' . $postnord_field_data . '" value="' . $postnord_field_data . '"></p>';

}
}

// save input from metabox
add_action( 'save_post', 'bdev_save_postnord_data_to_order', 10, 1 );
if ( ! function_exists( 'bdev_save_postnord_data_to_order' ) )
{

function bdev_save_postnord_data_to_order( $post_id ) {

if ( ! isset( $_POST[ 'postnord_meta_field_nonce' ] ) ) {
return $post_id;
}
$nonce = $_REQUEST[ 'postnord_meta_field_nonce' ];

if ( ! wp_verify_nonce( $nonce ) ) {
return $post_id;
}

if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}

if ( 'page' == $_POST[ 'post_type' ] ) {

if ( ! current_user_can( 'edit_page', $post_id ) ) {
return $post_id;
}
} else {

if ( ! current_user_can( 'edit_post', $post_id ) ) {
return $post_id;
}
}
update_post_meta( $post_id, '_postnord_field_data', $_POST[ 'postnord_data_name' ] );
}
}


// print tracking info under shipping address
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'postnord_custom_field_display_admin_order_meta', 10, 1 );
function postnord_custom_field_display_admin_order_meta($order){
$postnord_id_field = get_post_meta( $order->id, '_postnord_field_data', true );
if ( ! empty( $postnord_id_field ) ) {
echo '<p><strong>'. __("PostNord Tracking ID", "woocommerce").':</strong> ' . get_post_meta( $order->id, '_postnord_field_data', true ) . '</p>';
}
}


// add information to order email
add_action( 'woocommerce_email_after_order_table', 'add_postnord_tracking_to_customer_complete_order_email', 20, 2 );
function add_postnord_tracking_to_customer_complete_order_email( $order, $sent_to_admin ) {

if ( ! $sent_to_admin ) {
echo '<h2>Track Your Order</h2>';
echo '<p><strong>'. __("PostNord Tracking ID", "woocommerce").':</strong> ' . get_post_meta( $order->id, '_postnord_field_data', true ) . '</p>';
echo '<p>You can track your parcel on the <a href="https://www.postnord.se/en/online-tools/tools/track/track-and-trace" target="_blank" rel="">PostNord website</a> or directly from <a href="#" target="_blank" rel="">our site</a>.<br><br>';
}
}


The number is saved to the order and I would like to link it so that the customer can click it. When clicked, it should open a URL followed by the tracking number, like this: https://example.com/tracking-page/trackingnr



Any help is appreciated.







php woocommerce tracking orders email-notifications






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 16 '18 at 18:45









LoicTheAztec

85.3k136095




85.3k136095










asked Sep 16 '18 at 16:55









bjornenbjornen

295




295












  • Updated the save_post hooked function for a better one made for orders.
    – LoicTheAztec
    Sep 16 '18 at 22:17


















  • Updated the save_post hooked function for a better one made for orders.
    – LoicTheAztec
    Sep 16 '18 at 22:17
















Updated the save_post hooked function for a better one made for orders.
– LoicTheAztec
Sep 16 '18 at 22:17




Updated the save_post hooked function for a better one made for orders.
– LoicTheAztec
Sep 16 '18 at 22:17












1 Answer
1






active

oldest

votes


















2














I have revisited and simplified your code… There is some errors and mistakes like $order->id that should be $order->get_id() since Woocommerce 3.



In the last function your direct tracking Urls is now correctly set and you will have to just change https://example.com/ by the correct domain name and path.



The complete code:



// Add a the metabox to Order edit pages
add_action( 'add_meta_boxes', 'add_postnord_meta_box' );
function add_postnord_meta_box(){
add_meta_box( 'postnord_field', __('PostNord Parcel ID','woocommerce'), 'add_postnord_meta_box_content', 'shop_order', 'side', 'core' );

}

// The Metabow content
function add_postnord_meta_box_content(){
global $post;

$value = get_post_meta( $post->ID, '_postnord_field_data', true );

echo '<input type="hidden" name="postnord_meta_field_nonce" value="' . wp_create_nonce() . '">
<p style="border-bottom:solid 1px #eee;padding-bottom:13px;">
<input type="text" style="width:250px;";" name="postnord_data_name" value="'.$value.'"></p>';
}

// Save the field value from metabox content
add_action( 'save_post_shop_order', 'save_postnord_meta_box_field_value', 10, 1 );
function save_postnord_meta_box_field_value( $post_id ) {

if ( ! isset( $_POST[ 'postnord_meta_field_nonce' ] ) ) {
return $post_id;
}

$nonce = $_REQUEST[ 'postnord_meta_field_nonce' ];

if ( ! wp_verify_nonce( $nonce ) ) {
return $post_id;
}

if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}

if ( ! ( current_user_can( 'edit_shop_order', $post_id ) || current_user_can( 'edit_shop_order', $post_id ) ) ) {
return $post_id;
}

update_post_meta( $post_id, '_postnord_field_data', sanitize_text_field( $_POST[ 'postnord_data_name' ] ) );
}

// Display post north tracking info under shipping address
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'postnord_custom_field_display_admin_order_meta', 10, 1 );
function postnord_custom_field_display_admin_order_meta( $order ){
$postnord_value = $order->get_meta( '_postnord_field_data' );
if ( ! empty($postnord_value) ) {
echo '<p><strong>'. __("PostNord Tracking ID", "woocommerce").':</strong> ' . $postnord_value . '</p>';
}
}

// Display post north tracking info and urls on customer email
add_action( 'woocommerce_email_after_order_table', 'add_postnord_tracking_to_customer_complete_order_email', 20, 4 );
function add_postnord_tracking_to_customer_complete_order_email( $order, $sent_to_admin, $plain_text, $email ) {
if ( $sent_to_admin )
return; // Exit

$postnord_value = $order->get_meta( '_postnord_field_data' );

if ( ! empty($postnord_value) ) {
$postnord_url = 'https://www.postnord.se/en/online-tools/tools/track/track-and-trace';
$tracking_url = 'https://example.com/tracking-page/'.$postnord_value;
$title = __("Track Your Order","woocommerce");
$message = '<p><strong>'. __("PostNord Tracking ID", "woocommerce").':</strong> ' . $postnord_value . '</p>
<p>'. sprintf( __("You can track your parcel on the %s or directly from %s", "woocommerce"),
'<a href="'.$postnord_url.'" target="_blank">'.__("PostNord website", "woocommerce").'</a>',
'<a href="'.$tracking_url.'" target="_blank">'.__("our site", "woocommerce").'</a>.</p>');

echo '<style>
.tracking table {width: 100%; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;
color: #737373; border: 1px solid #e4e4e4; margin-bottom:8px;}
.tracking table td{text-align: left; border-top-width: 4px; color: #737373; border: 1px solid #e4e4e4;
padding: 12px; padding-bottom: 4px;}
</style>
<div class="tracking">
<h2>' . $title . '</h2>
<table cellspacing="0" cellpadding="6">
<tr><td>'.$message.'</td></tr>
</table></div><br>';
}
}


Code goes in function.php file of your active child theme (or active theme). Tested and works.



You will get the following, with the correct link with the tracking number in a clean table:



enter image description here






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52356373%2fset-a-tracking-number-link-on-customer-email-notifications-in-woocommerce-3%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    I have revisited and simplified your code… There is some errors and mistakes like $order->id that should be $order->get_id() since Woocommerce 3.



    In the last function your direct tracking Urls is now correctly set and you will have to just change https://example.com/ by the correct domain name and path.



    The complete code:



    // Add a the metabox to Order edit pages
    add_action( 'add_meta_boxes', 'add_postnord_meta_box' );
    function add_postnord_meta_box(){
    add_meta_box( 'postnord_field', __('PostNord Parcel ID','woocommerce'), 'add_postnord_meta_box_content', 'shop_order', 'side', 'core' );

    }

    // The Metabow content
    function add_postnord_meta_box_content(){
    global $post;

    $value = get_post_meta( $post->ID, '_postnord_field_data', true );

    echo '<input type="hidden" name="postnord_meta_field_nonce" value="' . wp_create_nonce() . '">
    <p style="border-bottom:solid 1px #eee;padding-bottom:13px;">
    <input type="text" style="width:250px;";" name="postnord_data_name" value="'.$value.'"></p>';
    }

    // Save the field value from metabox content
    add_action( 'save_post_shop_order', 'save_postnord_meta_box_field_value', 10, 1 );
    function save_postnord_meta_box_field_value( $post_id ) {

    if ( ! isset( $_POST[ 'postnord_meta_field_nonce' ] ) ) {
    return $post_id;
    }

    $nonce = $_REQUEST[ 'postnord_meta_field_nonce' ];

    if ( ! wp_verify_nonce( $nonce ) ) {
    return $post_id;
    }

    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
    return $post_id;
    }

    if ( ! ( current_user_can( 'edit_shop_order', $post_id ) || current_user_can( 'edit_shop_order', $post_id ) ) ) {
    return $post_id;
    }

    update_post_meta( $post_id, '_postnord_field_data', sanitize_text_field( $_POST[ 'postnord_data_name' ] ) );
    }

    // Display post north tracking info under shipping address
    add_action( 'woocommerce_admin_order_data_after_shipping_address', 'postnord_custom_field_display_admin_order_meta', 10, 1 );
    function postnord_custom_field_display_admin_order_meta( $order ){
    $postnord_value = $order->get_meta( '_postnord_field_data' );
    if ( ! empty($postnord_value) ) {
    echo '<p><strong>'. __("PostNord Tracking ID", "woocommerce").':</strong> ' . $postnord_value . '</p>';
    }
    }

    // Display post north tracking info and urls on customer email
    add_action( 'woocommerce_email_after_order_table', 'add_postnord_tracking_to_customer_complete_order_email', 20, 4 );
    function add_postnord_tracking_to_customer_complete_order_email( $order, $sent_to_admin, $plain_text, $email ) {
    if ( $sent_to_admin )
    return; // Exit

    $postnord_value = $order->get_meta( '_postnord_field_data' );

    if ( ! empty($postnord_value) ) {
    $postnord_url = 'https://www.postnord.se/en/online-tools/tools/track/track-and-trace';
    $tracking_url = 'https://example.com/tracking-page/'.$postnord_value;
    $title = __("Track Your Order","woocommerce");
    $message = '<p><strong>'. __("PostNord Tracking ID", "woocommerce").':</strong> ' . $postnord_value . '</p>
    <p>'. sprintf( __("You can track your parcel on the %s or directly from %s", "woocommerce"),
    '<a href="'.$postnord_url.'" target="_blank">'.__("PostNord website", "woocommerce").'</a>',
    '<a href="'.$tracking_url.'" target="_blank">'.__("our site", "woocommerce").'</a>.</p>');

    echo '<style>
    .tracking table {width: 100%; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;
    color: #737373; border: 1px solid #e4e4e4; margin-bottom:8px;}
    .tracking table td{text-align: left; border-top-width: 4px; color: #737373; border: 1px solid #e4e4e4;
    padding: 12px; padding-bottom: 4px;}
    </style>
    <div class="tracking">
    <h2>' . $title . '</h2>
    <table cellspacing="0" cellpadding="6">
    <tr><td>'.$message.'</td></tr>
    </table></div><br>';
    }
    }


    Code goes in function.php file of your active child theme (or active theme). Tested and works.



    You will get the following, with the correct link with the tracking number in a clean table:



    enter image description here






    share|improve this answer




























      2














      I have revisited and simplified your code… There is some errors and mistakes like $order->id that should be $order->get_id() since Woocommerce 3.



      In the last function your direct tracking Urls is now correctly set and you will have to just change https://example.com/ by the correct domain name and path.



      The complete code:



      // Add a the metabox to Order edit pages
      add_action( 'add_meta_boxes', 'add_postnord_meta_box' );
      function add_postnord_meta_box(){
      add_meta_box( 'postnord_field', __('PostNord Parcel ID','woocommerce'), 'add_postnord_meta_box_content', 'shop_order', 'side', 'core' );

      }

      // The Metabow content
      function add_postnord_meta_box_content(){
      global $post;

      $value = get_post_meta( $post->ID, '_postnord_field_data', true );

      echo '<input type="hidden" name="postnord_meta_field_nonce" value="' . wp_create_nonce() . '">
      <p style="border-bottom:solid 1px #eee;padding-bottom:13px;">
      <input type="text" style="width:250px;";" name="postnord_data_name" value="'.$value.'"></p>';
      }

      // Save the field value from metabox content
      add_action( 'save_post_shop_order', 'save_postnord_meta_box_field_value', 10, 1 );
      function save_postnord_meta_box_field_value( $post_id ) {

      if ( ! isset( $_POST[ 'postnord_meta_field_nonce' ] ) ) {
      return $post_id;
      }

      $nonce = $_REQUEST[ 'postnord_meta_field_nonce' ];

      if ( ! wp_verify_nonce( $nonce ) ) {
      return $post_id;
      }

      if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
      return $post_id;
      }

      if ( ! ( current_user_can( 'edit_shop_order', $post_id ) || current_user_can( 'edit_shop_order', $post_id ) ) ) {
      return $post_id;
      }

      update_post_meta( $post_id, '_postnord_field_data', sanitize_text_field( $_POST[ 'postnord_data_name' ] ) );
      }

      // Display post north tracking info under shipping address
      add_action( 'woocommerce_admin_order_data_after_shipping_address', 'postnord_custom_field_display_admin_order_meta', 10, 1 );
      function postnord_custom_field_display_admin_order_meta( $order ){
      $postnord_value = $order->get_meta( '_postnord_field_data' );
      if ( ! empty($postnord_value) ) {
      echo '<p><strong>'. __("PostNord Tracking ID", "woocommerce").':</strong> ' . $postnord_value . '</p>';
      }
      }

      // Display post north tracking info and urls on customer email
      add_action( 'woocommerce_email_after_order_table', 'add_postnord_tracking_to_customer_complete_order_email', 20, 4 );
      function add_postnord_tracking_to_customer_complete_order_email( $order, $sent_to_admin, $plain_text, $email ) {
      if ( $sent_to_admin )
      return; // Exit

      $postnord_value = $order->get_meta( '_postnord_field_data' );

      if ( ! empty($postnord_value) ) {
      $postnord_url = 'https://www.postnord.se/en/online-tools/tools/track/track-and-trace';
      $tracking_url = 'https://example.com/tracking-page/'.$postnord_value;
      $title = __("Track Your Order","woocommerce");
      $message = '<p><strong>'. __("PostNord Tracking ID", "woocommerce").':</strong> ' . $postnord_value . '</p>
      <p>'. sprintf( __("You can track your parcel on the %s or directly from %s", "woocommerce"),
      '<a href="'.$postnord_url.'" target="_blank">'.__("PostNord website", "woocommerce").'</a>',
      '<a href="'.$tracking_url.'" target="_blank">'.__("our site", "woocommerce").'</a>.</p>');

      echo '<style>
      .tracking table {width: 100%; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;
      color: #737373; border: 1px solid #e4e4e4; margin-bottom:8px;}
      .tracking table td{text-align: left; border-top-width: 4px; color: #737373; border: 1px solid #e4e4e4;
      padding: 12px; padding-bottom: 4px;}
      </style>
      <div class="tracking">
      <h2>' . $title . '</h2>
      <table cellspacing="0" cellpadding="6">
      <tr><td>'.$message.'</td></tr>
      </table></div><br>';
      }
      }


      Code goes in function.php file of your active child theme (or active theme). Tested and works.



      You will get the following, with the correct link with the tracking number in a clean table:



      enter image description here






      share|improve this answer


























        2












        2








        2






        I have revisited and simplified your code… There is some errors and mistakes like $order->id that should be $order->get_id() since Woocommerce 3.



        In the last function your direct tracking Urls is now correctly set and you will have to just change https://example.com/ by the correct domain name and path.



        The complete code:



        // Add a the metabox to Order edit pages
        add_action( 'add_meta_boxes', 'add_postnord_meta_box' );
        function add_postnord_meta_box(){
        add_meta_box( 'postnord_field', __('PostNord Parcel ID','woocommerce'), 'add_postnord_meta_box_content', 'shop_order', 'side', 'core' );

        }

        // The Metabow content
        function add_postnord_meta_box_content(){
        global $post;

        $value = get_post_meta( $post->ID, '_postnord_field_data', true );

        echo '<input type="hidden" name="postnord_meta_field_nonce" value="' . wp_create_nonce() . '">
        <p style="border-bottom:solid 1px #eee;padding-bottom:13px;">
        <input type="text" style="width:250px;";" name="postnord_data_name" value="'.$value.'"></p>';
        }

        // Save the field value from metabox content
        add_action( 'save_post_shop_order', 'save_postnord_meta_box_field_value', 10, 1 );
        function save_postnord_meta_box_field_value( $post_id ) {

        if ( ! isset( $_POST[ 'postnord_meta_field_nonce' ] ) ) {
        return $post_id;
        }

        $nonce = $_REQUEST[ 'postnord_meta_field_nonce' ];

        if ( ! wp_verify_nonce( $nonce ) ) {
        return $post_id;
        }

        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return $post_id;
        }

        if ( ! ( current_user_can( 'edit_shop_order', $post_id ) || current_user_can( 'edit_shop_order', $post_id ) ) ) {
        return $post_id;
        }

        update_post_meta( $post_id, '_postnord_field_data', sanitize_text_field( $_POST[ 'postnord_data_name' ] ) );
        }

        // Display post north tracking info under shipping address
        add_action( 'woocommerce_admin_order_data_after_shipping_address', 'postnord_custom_field_display_admin_order_meta', 10, 1 );
        function postnord_custom_field_display_admin_order_meta( $order ){
        $postnord_value = $order->get_meta( '_postnord_field_data' );
        if ( ! empty($postnord_value) ) {
        echo '<p><strong>'. __("PostNord Tracking ID", "woocommerce").':</strong> ' . $postnord_value . '</p>';
        }
        }

        // Display post north tracking info and urls on customer email
        add_action( 'woocommerce_email_after_order_table', 'add_postnord_tracking_to_customer_complete_order_email', 20, 4 );
        function add_postnord_tracking_to_customer_complete_order_email( $order, $sent_to_admin, $plain_text, $email ) {
        if ( $sent_to_admin )
        return; // Exit

        $postnord_value = $order->get_meta( '_postnord_field_data' );

        if ( ! empty($postnord_value) ) {
        $postnord_url = 'https://www.postnord.se/en/online-tools/tools/track/track-and-trace';
        $tracking_url = 'https://example.com/tracking-page/'.$postnord_value;
        $title = __("Track Your Order","woocommerce");
        $message = '<p><strong>'. __("PostNord Tracking ID", "woocommerce").':</strong> ' . $postnord_value . '</p>
        <p>'. sprintf( __("You can track your parcel on the %s or directly from %s", "woocommerce"),
        '<a href="'.$postnord_url.'" target="_blank">'.__("PostNord website", "woocommerce").'</a>',
        '<a href="'.$tracking_url.'" target="_blank">'.__("our site", "woocommerce").'</a>.</p>');

        echo '<style>
        .tracking table {width: 100%; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;
        color: #737373; border: 1px solid #e4e4e4; margin-bottom:8px;}
        .tracking table td{text-align: left; border-top-width: 4px; color: #737373; border: 1px solid #e4e4e4;
        padding: 12px; padding-bottom: 4px;}
        </style>
        <div class="tracking">
        <h2>' . $title . '</h2>
        <table cellspacing="0" cellpadding="6">
        <tr><td>'.$message.'</td></tr>
        </table></div><br>';
        }
        }


        Code goes in function.php file of your active child theme (or active theme). Tested and works.



        You will get the following, with the correct link with the tracking number in a clean table:



        enter image description here






        share|improve this answer














        I have revisited and simplified your code… There is some errors and mistakes like $order->id that should be $order->get_id() since Woocommerce 3.



        In the last function your direct tracking Urls is now correctly set and you will have to just change https://example.com/ by the correct domain name and path.



        The complete code:



        // Add a the metabox to Order edit pages
        add_action( 'add_meta_boxes', 'add_postnord_meta_box' );
        function add_postnord_meta_box(){
        add_meta_box( 'postnord_field', __('PostNord Parcel ID','woocommerce'), 'add_postnord_meta_box_content', 'shop_order', 'side', 'core' );

        }

        // The Metabow content
        function add_postnord_meta_box_content(){
        global $post;

        $value = get_post_meta( $post->ID, '_postnord_field_data', true );

        echo '<input type="hidden" name="postnord_meta_field_nonce" value="' . wp_create_nonce() . '">
        <p style="border-bottom:solid 1px #eee;padding-bottom:13px;">
        <input type="text" style="width:250px;";" name="postnord_data_name" value="'.$value.'"></p>';
        }

        // Save the field value from metabox content
        add_action( 'save_post_shop_order', 'save_postnord_meta_box_field_value', 10, 1 );
        function save_postnord_meta_box_field_value( $post_id ) {

        if ( ! isset( $_POST[ 'postnord_meta_field_nonce' ] ) ) {
        return $post_id;
        }

        $nonce = $_REQUEST[ 'postnord_meta_field_nonce' ];

        if ( ! wp_verify_nonce( $nonce ) ) {
        return $post_id;
        }

        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return $post_id;
        }

        if ( ! ( current_user_can( 'edit_shop_order', $post_id ) || current_user_can( 'edit_shop_order', $post_id ) ) ) {
        return $post_id;
        }

        update_post_meta( $post_id, '_postnord_field_data', sanitize_text_field( $_POST[ 'postnord_data_name' ] ) );
        }

        // Display post north tracking info under shipping address
        add_action( 'woocommerce_admin_order_data_after_shipping_address', 'postnord_custom_field_display_admin_order_meta', 10, 1 );
        function postnord_custom_field_display_admin_order_meta( $order ){
        $postnord_value = $order->get_meta( '_postnord_field_data' );
        if ( ! empty($postnord_value) ) {
        echo '<p><strong>'. __("PostNord Tracking ID", "woocommerce").':</strong> ' . $postnord_value . '</p>';
        }
        }

        // Display post north tracking info and urls on customer email
        add_action( 'woocommerce_email_after_order_table', 'add_postnord_tracking_to_customer_complete_order_email', 20, 4 );
        function add_postnord_tracking_to_customer_complete_order_email( $order, $sent_to_admin, $plain_text, $email ) {
        if ( $sent_to_admin )
        return; // Exit

        $postnord_value = $order->get_meta( '_postnord_field_data' );

        if ( ! empty($postnord_value) ) {
        $postnord_url = 'https://www.postnord.se/en/online-tools/tools/track/track-and-trace';
        $tracking_url = 'https://example.com/tracking-page/'.$postnord_value;
        $title = __("Track Your Order","woocommerce");
        $message = '<p><strong>'. __("PostNord Tracking ID", "woocommerce").':</strong> ' . $postnord_value . '</p>
        <p>'. sprintf( __("You can track your parcel on the %s or directly from %s", "woocommerce"),
        '<a href="'.$postnord_url.'" target="_blank">'.__("PostNord website", "woocommerce").'</a>',
        '<a href="'.$tracking_url.'" target="_blank">'.__("our site", "woocommerce").'</a>.</p>');

        echo '<style>
        .tracking table {width: 100%; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;
        color: #737373; border: 1px solid #e4e4e4; margin-bottom:8px;}
        .tracking table td{text-align: left; border-top-width: 4px; color: #737373; border: 1px solid #e4e4e4;
        padding: 12px; padding-bottom: 4px;}
        </style>
        <div class="tracking">
        <h2>' . $title . '</h2>
        <table cellspacing="0" cellpadding="6">
        <tr><td>'.$message.'</td></tr>
        </table></div><br>';
        }
        }


        Code goes in function.php file of your active child theme (or active theme). Tested and works.



        You will get the following, with the correct link with the tracking number in a clean table:



        enter image description here







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Sep 16 '18 at 22:14

























        answered Sep 16 '18 at 18:31









        LoicTheAztecLoicTheAztec

        85.3k136095




        85.3k136095






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52356373%2fset-a-tracking-number-link-on-customer-email-notifications-in-woocommerce-3%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            TypeError: fit_transform() missing 1 required positional argument: 'X'