nchopra
nchopra

Reputation: 631

Show order downloads in admin new order email notification in WooCommerce

How can I include this "Downloads" section (attached) in the admin email? By default, WooCommerce only sends it to the customer, not the store owner.

I tried looking at articles that showed how to customize WooCommerce emails and I think that woocommerce_email_order_details is the hook that I'm looking for. However, I am stuck with just this piece of information as I cannot find how to actually use this hook to change the contents of an email notification.

Furthermore, I also looked at the email templates included by WooCommerce: I noticed that both the Customer notification and the Admin notification have this line do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email );

It is identical in both, and I assume it behaves differently depending on whether or not $sent_to_admin is True; however again, I haven't been able to find how I can use this to have the email include the Downloads section in both the customer and admin email.

Any advice?

enter image description here

Upvotes: 3

Views: 1315

Answers (3)

7uc1f3r
7uc1f3r

Reputation: 29640

This answer contains a solution, without overwriting template files


In includes/class-wc-emails.php we can find

/**
 * Constructor for the email class hooks in all emails that can be sent.
 */
public function __construct() {
    $this->init();

    // Email Header, Footer and content hooks.
    add_action( 'woocommerce_email_header', array( $this, 'email_header' ) );
    add_action( 'woocommerce_email_footer', array( $this, 'email_footer' ) );
    add_action( 'woocommerce_email_order_details', array( $this, 'order_downloads' ), 10, 4 );
    ...

As you can see the add_action contains a callback to the order_downloads() function.

/**
 * Show order downloads in a table.
 *
 * @since 3.2.0
 * @param WC_Order $order         Order instance.
 * @param bool     $sent_to_admin If should sent to admin.
 * @param bool     $plain_text    If is plain text email.
 * @param string   $email         Email address.
 */
public function order_downloads( $order, $sent_to_admin = false, $plain_text = false, $email = '' ) {
    $show_downloads = $order->has_downloadable_item() && $order->is_download_permitted() && ! $sent_to_admin && ! is_a( $email, 'WC_Email_Customer_Refunded_Order' );

    if ( ! $show_downloads ) {
        return;
    }

This function contains a condition $show_downloads, it must be true to show order downloads in a table. So should $sent_to_admin be false, to meet your demand.


So to answer your question. Without overwriting template files, use:

// Let 3rd parties unhook via this hook.
function action_woocommerce_email( $emails ) {
    // Removes a function from a specified action hook.
    remove_action( 'woocommerce_email_order_details', array( $emails, 'order_downloads' ), 10 );
    
    // Hooks a function on to a specific action.
    add_action( 'woocommerce_email_order_details', 'action_woocommerce_email_order_details', 9, 4 );
}
add_action( 'woocommerce_email', 'action_woocommerce_email', 10, 1 );

/**
 * Show order downloads in a table.
 *
 * @since 3.2.0
 * @param WC_Order $order         Order instance.
 * @param bool     $sent_to_admin If should sent to admin.
 * @param bool     $plain_text    If is plain text email.
 * @param string   $email         Email address.
 */
function action_woocommerce_email_order_details( $order, $sent_to_admin = false, $plain_text = false, $email = '' ) {   
    // Only for 'New Order' email notifications
    if ( $email->id == 'new_order' ) {
        $sent_to_admin = false;
    }

    $show_downloads = $order->has_downloadable_item() && $order->is_download_permitted() && ! $sent_to_admin && ! is_a( $email, 'WC_Email_Customer_Refunded_Order' );

    if ( ! $show_downloads ) {
        return;
    }

    $downloads = $order->get_downloadable_items();
    
    $columns   = apply_filters(
        'woocommerce_email_downloads_columns',
        array(
            'download-product' => __( 'Product', 'woocommerce' ),
            'download-expires' => __( 'Expires', 'woocommerce' ),
            'download-file'    => __( 'Download', 'woocommerce' ),
        )
    );

    if ( $plain_text ) {
        wc_get_template(
            'emails/plain/email-downloads.php',
            array(
                'order'         => $order,
                'sent_to_admin' => $sent_to_admin,
                'plain_text'    => $plain_text,
                'email'         => $email,
                'downloads'     => $downloads,
                'columns'       => $columns,
            )
        );
    } else {
        wc_get_template(
            'emails/email-downloads.php',
            array(
                'order'         => $order,
                'sent_to_admin' => $sent_to_admin,
                'plain_text'    => $plain_text,
                'email'         => $email,
                'downloads'     => $downloads,
                'columns'       => $columns,
            )
        );
    }
}

Upvotes: 2

Yeasir Muhmmad Pervej
Yeasir Muhmmad Pervej

Reputation: 161

Download order template is loaded by using this function located at /wp-content/plugins/woocommerce/includes/class-wc-emails.php

public function order_downloads( $order, $sent_to_admin = false, $plain_text = false, $email = '' ) {
    $show_downloads = $order->has_downloadable_item() && $order->is_download_permitted() && ! $sent_to_admin && ! is_a( $email, 'WC_Email_Customer_Refunded_Order' );

    if ( ! $show_downloads ) {
        return;
    }
.....

as you can see, you need to make the conditions to be true. If all other conditions are true, then you just need to make the "$sent_to_admin" value to false. Which you can achieve by updating the following file located at /wp-content/themes/your-theme/woocommerce/emails/admin-new-order.php

Replace

 do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email );

To

$store_sent_to_admin = $sent_to_admin;
$sent_to_admin = false;
do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email );
$sent_to_admin = $store_sent_to_admin;

Here I have stored $sent_to_admin default value thinking you might have custom order meta or email footer section for admin only. Although I have not tested this on any website, but I hope this will work fine.

Upvotes: 0

Diego
Diego

Reputation: 1716

Looking at Woocommerce code you can find in: wp-content/plugins/woocommerce/includes/class-wc-emails.php this piece of code:

/**
 * Show order downloads in a table.
 *
 * @since 3.2.0
 * @param WC_Order $order         Order instance.
 * @param bool     $sent_to_admin If should sent to admin.
 * @param bool     $plain_text    If is plain text email.
 * @param string   $email         Email address.
 */
public function order_downloads( $order, $sent_to_admin = false, $plain_text = false, $email = '' ) {
    $show_downloads = $order->has_downloadable_item() && $order->is_download_permitted() && ! $sent_to_admin && ! is_a( $email, 'WC_Email_Customer_Refunded_Order' );

    if ( ! $show_downloads ) {
        return;
    }
    ...

Which leads exactly to what you think, $sent_to_admin is responsible for the download part to appear or not to appear.

If you want that to appear in the admin order email i think the easiest way to achieve this would be to do something like this:

  1. Copy wp-content/plugins/woocommerce/templates/emails/admin-new-order.php inside your theme/woocommerce/emails folder
  2. Change this:

do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email );

to this:

// Forcing email to be like the one the customer receives
do_action( 'woocommerce_email_order_details', $order, false, $plain_text, $email );

This should make the difference

Upvotes: 1

Related Questions