'Remove product downloads section in woocommerce email notifications
I would like to remove the download section from the email template that woocommerce will send after my buyer has placed an order.
It doesn’t make sense to send the download link to my buyer when their order is still on hold, processing and refunded state.
Refer image below:
my current email template for on hold status:
<?php
/**
* Customer on-hold order email
*
* This template can be overridden by copying it to yourtheme/woocommerce/emails/customer-on-hold-order.php.
*
* HOWEVER, on occasion WooCommerce will need to update template files and you
* (the theme developer) will need to copy the new files to your theme to
* maintain compatibility. We try to do this as little as possible, but it does
* happen. When this occurs the version of the template file will be bumped and
* the readme will list any important changes.
*
* @see https://docs.woocommerce.com/document/template-structure/
* @author WooThemes
* @package WooCommerce/Templates/Emails
* @version 2.5.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* @hooked WC_Emails::email_header() Output the email header
*/
do_action( 'woocommerce_email_header', $email_heading, $email ); ?>
<p><?php _e( "Your order is on-hold until we confirm payment has been received. Your order details are shown below for your reference:", 'woocommerce' ); ?></p>
<?php
/**
* @hooked WC_Emails::order_meta() Shows order meta data.
*/
do_action( 'woocommerce_email_order_meta', $order, $sent_to_admin, $plain_text, $email );
/**
* @hooked WC_Emails::customer_details() Shows customer details
* @hooked WC_Emails::email_address() Shows email address
*/
do_action( 'woocommerce_email_customer_details', $order, $sent_to_admin, $plain_text, $email );
/**
* @hooked WC_Emails::email_footer() Output the email footer
*/
do_action( 'woocommerce_email_footer', $email );
I still want to retain the order details, I just wanted to remove the download section from the email.
Any help will be highly appreciated.
Solution 1:[1]
At least since Woocommerce version 3.2 normally products downloads section only appears when order status is completed or when shop manager allows downloads for the order. So there is something wrong in your Woocommerce installation.
Now what you can do if you can't solve the problem, is to remove the original hooked function that displays the products downloads section in email notification and replace it with a similar one that will allow to display the products downloads section only when order status is completed.
The code:
add_action( 'woocommerce_email', 'remove_email_order_downloads', 10, 1 );
function remove_email_order_downloads( $emails ){
remove_action( 'woocommerce_email_order_details', array( $emails, 'order_downloads' ), 10 );
add_action( 'woocommerce_email_order_details', 'custom_order_downloads', 9, 4 );
}
function custom_order_downloads( $order, $sent_to_admin = false, $plain_text = false, $email = '' ) {
$show_downloads = $order->has_downloadable_item() && $order->is_download_permitted() && ! $sent_to_admin && $order->has_status('completed');
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 ) );
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |