Skip to content
Advertisement

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

Advertisement

Answer

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,
            )
        );
    }
}
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement