slint slint - 3 months ago 51
PHP Question

Delete order info section from email template in woocommerce

I'm trying to delete the Order Information section on a Completed Order and Customer Invoice emails.

enter image description here

Can't find how to delete this in the:

wp-content/plugins/woocommerce/templates/emails/customer-completed-order.php


-



<?php
/**
* Subscription information template
*
* @author Brent Shepherd / Chuck Mac
* @package WooCommerce_Subscriptions/Templates/Emails
* @version 1.5
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
?>
<?php if ( ! empty( $subscriptions ) ) : ?>
<h2><?php esc_html_e( 'Order Information:', 'woocommerce-subscriptions' ) ?></h2>
<table cellspacing="0" cellpadding="6" style="width: 100%; border: 1px solid #eee;" border="1" bordercolor="#eee">
<thead>
<tr>
<th scope="col" style="text-align:left; border: 1px solid #eee;"><?php esc_html_e( 'Order', 'woocommerce-subscriptions' ); ?></th>
<th scope="col" style="text-align:left; border: 1px solid #eee;"><?php esc_html_e( 'Start Date', 'woocommerce-subscriptions' ); ?></th>
<th scope="col" style="text-align:left; border: 1px solid #eee;"><?php esc_html_e( 'End Date', 'woocommerce-subscriptions' ); ?></th>
<th scope="col" style="text-align:left; border: 1px solid #eee;"><?php esc_html_e( 'Price', 'woocommerce-subscriptions' ); ?></th>
</tr>
</thead>
<tbody>
<?php foreach ( $subscriptions as $subscription ) : ?>
<tr>
<td scope="row" style="text-align:left; border: 1px solid #eee;"><a href="<?php echo esc_url( ( $is_admin_email ) ? wcs_get_edit_post_link( $subscription->id ) : $subscription->get_view_order_url() ); ?>"><?php echo esc_html( $subscription->get_order_number() ); ?></a></td>
<td scope="row" style="text-align:left; border: 1px solid #eee;"><?php echo esc_html( date_i18n( wc_date_format(), $subscription->get_time( 'start', 'site' ) ) ); ?></td>
<td scope="row" style="text-align:left; border: 1px solid #eee;"><?php echo esc_html( ( 0 < $subscription->get_time( 'end' ) ) ? date_i18n( wc_date_format(), $subscription->get_time( 'end', 'site' ) ) : __( 'When Cancelled', 'woocommerce-subscriptions' ) ); ?></td>
<td scope="row" style="text-align:left; border: 1px solid #eee;"><?php echo wp_kses_post( $subscription->get_formatted_order_total() ); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>





Please advise.
Thanks.

Answer

You don't want to remove the entire action hook. This could effect other plugins that rely on it's presence. (Not to mention the order details are on the woocommerce_email_order_details details hook and not the woocommerce_email_order_meta hook anyway).

The proper way to remove the order details from all emails is to remove their callback function which is WC_Emails::order_details() which is added to the woocommerce_email_order_details hook here

You cannot call remove_action() directly and so must call it from inside an add_action() callback... after it has been added to it's hook, but before it has been run. In this case we're just going to sneak onto the same hook, but with an earlier priority. Also note, that remove_action() must have the same priority as the add_action() that you are trying to remove.

function so_39251827_remove_order_details( $order, $sent_to_admin, $plain_text, $email ){
    $mailer = WC()->mailer(); // get the instance of the WC_Emails class
    remove_action( 'woocommerce_email_order_details', array( $mailer, 'order_details' ), 10, 4 );
}
add_action( 'woocommerce_email_order_details', 'so_39251827_remove_order_details', 5, 4 );

EDIT: Instructions for use

  1. Stop overriding the emails/customer-completed-order.php template and remove it from your theme. You do not need to edit it directly to solve this question.
  2. Add the above code block to your theme's functions.php or preferably a custom-snippets plugin, such as this one.

EDIT #2: Remove Subscriptions Info Only

Replace the above code with the following:

function so_39251827_remove_subscription_details( $order, $sent_to_admin, $plain_text, $email ){
    remove_action( 'woocommerce_email_after_order_table', array( 'WC_Subscriptions_Order', 'add_sub_info_email' ), 15, 3 );
}
add_action( 'woocommerce_email_after_order_table', 'so_39251827_remove_subscription_details', 5, 4 );
Comments