Add a Link Back to the Order in WooCommerce New Order Notifications Email

In this quick tutorial I will show you how to add a link to the order edit page on the WooCommerce New Order email notifications, to the store administrator. It is a very simple, yet useful link to have since you can just click on it and go to the order, right from within your email. As I said, it is very simple to do, so I will just go right ahead and explain it.

Find the action hook

First thing we want to do is find the action hook we want to attach the function, which will output the link to. There are a few hooks that can work:

  • woocommerce_email_before_order_table – will output before the Order details
  • woocommerce_email_after_order_table – will output after the Order Details
  • woocommerce_email_order_meta –Β  will output after the Order Details and after the woocommerce_email_after_order_table hook.

Just pick one and lets continue.

Add the hook and function to the function.php file

Now that you picked the hook you want to output the link, just open your theme’s functions.php file and paste this at the end of the file.

As you can see I picked the woocommerce_email_after_order_table hook and added a function to it.
The function takes two parameters, the order object and if the email is to be send to the admin as a Boolean value.Β  There is not much room for a breakdown here, all we do is check to make sure the admin:

        // Only for admin emails
	if ( ! $is_admin ) {
		return;
	}

Then build the anchor link linking to the Order Edit page. Save it and place a new order. The link should look something like this:
order-notification-email

That’s it. Hope you found this tutorial useful. If you have an idea of a tutorial you want to see, just let me know in the comments below.

Comments 39

  1. Anders Sandfuss

    Hi Vanbo.

    First of all big thanks to helping out.

    Im using the code below, which you wrote below in another post. That works just great – the title in the order confirmation email links perfectly to the product. So far so good.

    Now what I would like, is the title to link to a specific listing (and not the product itself) where this product is listed.

    What code would I need for this?

    Just to show you one example the permalink of a listing is: http://noborderfitness.com/listing/kobenhavn-emdrup-bad-bredelandsvej/

    Here is the code you wrote that im using now:

    add_filter( ‘woocommerce_order_item_name’, ‘display_product_title_as_link’, 10, 2 );
    function display_product_title_as_link( $item_name, $item ) {

    $_product = get_product( $item[‘variation_id’] ? $item[‘variation_id’] : $item[‘product_id’] );

    $link = get_permalink( $_product->id );

    return ‘‘. $item_name .’‘;

    }

    THANKS A LOT – I really appreciate it.
    The website is http://noborderfitness.com/
    but it is not public yet

  2. Mauro Carrera

    Hi guys! I really need your help.
    Im dealing with a big issue.

    I need to access our Orders with no admin access. Just with a random permalink. This is posible? How can i do this?

    Thank you very much!

  3. Luuk

    Hi Vanbo,

    I’ve been trying to figure out a way to add the order notes in to the “Order Completed” email. I have found some code online but nothing has worked. Do you have any idea how I could make this work? It sounds simple but can’t get it to work. This way, I could easily add the shipment tracking information by simply typing a note.

    Any help is much appreciated!

    Luuk

    1. Post
      Author
      vanbo

      Hi Luuk,

      You already have the WC_Order object passed to the hooks, all you have to do is take all customer notes and display them. $order->get_customer_order_notes(); will get you an array of all notes. Just loop through them and display any info you want.

  4. Alessandro

    Hello,
    I have installed woocommerce with the ability to add one product to cart and buy it.

    Now I would like to add the email that the customer receives a different custom link for each product (for creating an upsell offer) you can do this?

    Thank you

  5. John

    Can this be done for the email received by a customer who ordered via guest checkout? I would assume it would have the order key in the URL? (Ex. …?key=wc_order_123b12fd12d12).

    1. Post
      Author
      vanbo

      Hi John,

      Don’t see a reason why it can’t. I assume you want to point the link to the Thank You page? In any case, you have all the information you need, in order to build the correct link, in the $order object. All you need to do is know where you want to point the user to and build the link.

      Note: have in mind that if you want to point the user to a “View Order Page” it will most likely not work because WC requires credentials for all “My Account” pages

  6. Stephen

    Hi Vanbo. I’m wondering what code I would need to add to a new order notification e-mail, so that I, as an adminstrator, can add the permalink to the e-mail, so that it can take me straight to the product page from the admin e-mail. Thanks.

    1. Post
      Author
      vanbo

      Hi Stephen,

      you will need to use another hook “woocommerce_order_item_name”, which echoes the order item name. However, this hook does not provide the $sent_to_admin variable, so you don’t know which email you are manipulating.
      Or you can use 'woocommerce_order_item_meta_start' to just add an additional link to the product edit page. This one also does not have the $sent_to_admin variable, so you might be left with the only option of overwriting the template and adding the $sent_to_admin variable as the last passed variable to one of the hooks. It won’t affect the overall workings of the code, but it will pass the needed variable.

      I guess you can also keep it simpler and perform your own loop through the order items and add the links to each of the items edit page.

  7. Lodix

    Jeff Berman asked, in an earlier comment, about how to add a link to customer emails that will take the customer to see their order (myurl/my-account/view-order/xxx/). I was trying to do the same and struggled with that. He couldn’t get it to work. I finally managed it by adding this code to the email template:

    printf( __( ‘Order: %s’, ‘woocommerce’), ‘get_view_order_url() . ‘”>’. $order->get_order_number() .’‘ );

    I am just sharing in the hope to benefit others who want to do the same.

  8. Anthony T

    Hey ya. Great tip. If i wanted to add this to other notifications such as the customer note, can you share what i need to add to make that work? Cheers

    1. Post
      Author
      vanbo

      Hi Anthony,

      All you need to do is change the action you use. Instead of using “woocommerce_email_after_order_table” as it is shown in the example, use “woocommerce_email_customer_details” or any of the actions used in the perticular email template(just open the template and see what actions are called there).

  9. Mara

    Hello vanbo,
    thank you for the tutorial,very helpful!
    Do you know if it is possible to have the customer not be directed to Paypal when he chose this as the payment method, but instead have him directed to the regular thankyou page and then have a pa-now link to Paypal being inserted in the order-processed email?
    Looking forward to your answer,
    Mara

  10. Sab

    Hi,
    I would like to remove the bank account info that is displayed in the order-processing-email when chosen payment method is cash in advance. I know this is set though
    woocommerce_email_before_order_table – action
    and
    public function email_instructions( $order, $sent_to_admin, $plain_text = false ) {

    if ( ! $sent_to_admin && ‘bacs’ === $order->payment_method && $order->has_status( ‘on-hold’ ) ) {
    if ( $this->instructions ) {
    echo wpautop( wptexturize( $this->instructions ) ) . PHP_EOL;
    }
    $this->bank_details( $order->id );
    }

    }
    So only the bank details need to be removed, not the instructions. This should be possible with a remove_action, but I am just learning PHP, could you help me with this? I would greatly appreciate it. Thank you very much!

    1. Post
      Author
      1. Sab

        Hi,
        thank you so much for this! I thought it would be much simpler by just using a small remove_action, but I guess there is more involved πŸ™‚ Again, I cannot thank you enough!

  11. WC-zastore

    Thanks for this.
    I’m trying to add links to my email that let the customer update order statuses.
    For example when a new order is placed we add a link to the order confirmation emails to update the status to “approved” or “cancelled”. I have made custom statuses but don’t know to let the customer update them by simply clicking a link in the email? Can you help please?

    1. Post
      Author
      vanbo

      Hi WC-zastore,

      This link will need to point to an endpoint of your store, at which you will need to authenticate the user, checking if the user is logged in or having some sort of hashing or even other authentication to make sure only this user can change the order status. You will need to pass the order number, the status you need to change along with the authentication data, through the URL. After authentication, all you need is to change the order status to the one passed through the link.

  12. maximoau

    Hi Vanbo,
    I found you through a Google search,
    I cant seem to find an answer to this anywhere.
    I hope you are able to guide me as I am a new Woocommerce user.
    I have the latest version and am using a Verada child theme with a Woocommece templates folder in there.

    I’m stuck on how to display the product full description (the_content) in the admin email. I have worked out a way to show it in the single items page the Cart but not the order email sent to admin.
    Regards.

    1. Post
      Author
  13. Alex

    Hi Vanbo,
    I would like to add the link to the customer new order but I don’t know how to do that!

    Can you help me?
    Alex

    1. Post
      Author
      vanbo

      Hi Alex,

      Please note the admin check above:

          // Only for admin emails
          if ( ! $is_admin ) {
              return;
          }
      

      It is the one that prevents your link to go to the customer. If you want it sent to the customer only, you will need the check to be

      if ( $is_admin ) {

      . If you want it to both admin and customer just remove the whole code part.

      if you want the link added to the customer email just

  14. Jeff

    Do you know how I could make the Order: #xxx text that goes to the customer in their order e-mails link to the view order page for that order?

    myurl/my-account/view-order/xxx/

    1. Post
      Author
      vanbo

      Hi Jeff,

      yes, you can do that by surrounding the text with a link point to the View Order Url.

      The Url is in

      $order->get_view_order_url();

      . However the Order #xxx, does not have a filter to it, so you will have to transfer the “templates\emails\admin-new-order.php” template to your theme and edit this part from there.

      The correct line should be line 7, find the:

      printf( __( 'Order: %s', 'woocommerce'), $order->get_order_number() );

      and edit it to look like this:

      printf( __( 'Order: %s', 'woocommerce'), '<a href="$order->get_view_order_url()">'. $order->get_order_number() .'</a>' );
      

      It will link the #xxxx part to the view order page.

      1. Jeff

        Thank you very much for the fast reply. My apologies for not clarifying. My admin email already includes a functional link on Order: #XXX, which goes to myurl/wp-admin/post.php?post=XXX&action=edit.

        I basically want the same type of functionality for the e-mails the customer receives. Assuming this would be for customer-processing-order.php and customer-completed-order.php.

        Thanks again so much!

        1. Post
          Author
          vanbo

          Hi Jeff,

          the same thing applied for the other email templates. Just open the templates for the customer ( complete and processing ) emails and edit the $order->get_order_number() by putting a link around it as I showed you.

          1. Jeff Berman

            Somehow, I can’t seem to figure it out. lol πŸ™

            The native code is:

            get_order_number(); ?>

            I feel like I’ve tried a bunch of variations, but everything I do seems to not kick out an e-mail at all. Part of the problem is not being a coder, haha.

            Thanks again πŸ™‚

  15. Tamara

    Hi Vanbo,
    I would like to add the customer comments (order_comments) to the new order notifications e-mail, but I don’t know how to do that!
    I think I can use your code, but I don’t know exactly how to change the code for this.
    Can you help me?
    Tamara

    1. Post
      Author
      vanbo

      Hi Tamara,

      you have the $order object, so you can use the

      $comments = $order->get_customer_order_notes();

      to get the customer comments. It returns and array of objects for

      foreach ( $comments as $comment ) {
         echo $comment->comment_content;
      }
      
    1. Post
      Author
      vanbo

      I’ll be happy to help Silvio, but just “doesn’t work”, doesn’t tell me what you need help with.
      Just copy and paste the code above in your functions.php file and you will get the link.

      `echo` anything inside the function and it will appear on the Admin New Order email.

  16. Sohaib

    Hi Vanbo, my question is similar to this article, actually I want to add the product/item link in the Processing Order email which User receives when order. When an order is placed an Order Email sent, I want that product clickable and when user clicks it redirects to the detail product page. Is there any way, how the product title be clickable and moves to its detail page. Thanks

    1. Post
      Author
      vanbo

      Hi Sohaib, yes you can certainly send the title as a link. Just use the “woocommerce_order_item_name” filter.
      It should look something like this ( please test it before hand ).

      	add_filter( 'woocommerce_order_item_name', 'display_product_title_as_link', 10, 2 );
      	function display_product_title_as_link( $item_name, $item ) {
      
      		$_product = get_product( $item['variation_id'] ? $item['variation_id'] : $item['product_id'] );
      
      		$link = get_permalink( $_product->id );
      
      		return '<a href="'. $link .'"  rel="nofollow">'. $item_name .'</a>';
      
      	}
      
      1. Angelo

        Hi, thank for your help throught this post. I just wanted to add the variation description to the new order email but i cannot find how to do it, do you have any idea?

        In the deafult mail arrives only the “main” product description but not the variation description that i put in the product variation.

        Thank you very much

        1. Post
          Author
          vanbo

          Hi Angelo,

          There is a method get_variation_description(), which returns the variation description. It is only available for Variation product, so you will have to make sure that you are dealing with a variation and just echo this method.
          So in the example $_product will be your object, but you need to make sure that this is initialized with the “variation_id”(if there is one). Then just echo $_product->get_variation_description();

          1. Angelo

            Thank you v ery much for your response.

            I tried to implement the snippet but when i put it in function.php…it crashes… do you think is ok?

            add_filter( 'woocommerce_order_item_name', 'product_variation', 10, 2 );

            function product_variation ( $item_name, $item ) {

            if ( $item['variation_id'] ){
            $_product = get_product($item['variation_id']);
            $_final_name = $item_name . β€˜β€™ . $_product->get_variation_description();
            }else{
            $_product = get_product($item['product_id']);
            $_final_name = $item_name
            }
            return $_final_name;
            }

          2. Angelo

            THANK YOU! finally i win my little code battle! here is the final code:

            add_filter( 'woocommerce_order_item_name', 'display_product_title_as_link', 10, 2 );
            function display_product_title_as_link( $item_name, $item ) {
            $_product = get_product( $item['variation_id'] ? $item['variation_id'] : $item['product_id'] );
            $link = get_permalink( $_product->id );
            $_var_description ='';
            if ( $item['variation_id'] ) {
            $_var_description = $_product->get_variation_description();
            }
            return ''. $item_name .''. $_var_description ;
            }

Leave a Reply

Your email address will not be published. Required fields are marked *