Search code examples
phpwordpresswoocommerceproductemail-notifications

Remove product purchase notes in certain WooCommerce email notifications


I try to remove the purchase notes from the order completed mail. Currently the purchase notes are in the order confirmation mail and also in the order completed mail. But we do not want that info in the order completed mail.

So for that I found this input here: https://wordpress.stackexchange.com/questions/340045/how-do-i-hide-the-purchase-note-in-the-woocommerce-order-completed-email

I also found that snipped here below. But with that it removes it everywhere and not just in the order complete mail. Any advice?

add_filter('woocommerce_email_order_items_args','remove_order_note',12);
function remove_order_note($args){
    $args['show_purchase_note']= false;
    return $args;
}

Solution

  • You can use the email ID to target specific email notifications in WooCommerce. However, because this is not known in the hook you are using, we will make the email id globally available via another hook

    So you get:

    // Setting the email_is as a global variable
    function action_woocommerce_email_before_order_table( $order, $sent_to_admin, $plain_text, $email ) {           
        $GLOBALS['email_id_str'] = $email->id;
    }
    add_action( 'woocommerce_email_before_order_table', 'action_woocommerce_email_before_order_table', 10, 4 );
    
    function filter_woocommerce_email_order_items_args( $args ) {
        // Getting the email ID global variable
        $refNameGlobalsVar = $GLOBALS;
        $email_id = isset( $refNameGlobalsVar['email_id_str'] ) ? $refNameGlobalsVar['email_id_str'] : '';
    
        // Targeting specific email. Multiple statuses can be added, separated by a comma
        if ( in_array( $email_id, array( 'customer_completed_order', 'customer_invoice' ) ) ) {
            // False
            $args['show_purchase_note'] = false;
        }
    
        return $args;
    }
    add_filter( 'woocommerce_email_order_items_args', 'filter_woocommerce_email_order_items_args', 10, 1 );
    

    Note: How to target other WooCommerce order emails

    Used in this answer: Changing in WooCommerce email notification Order Item table "Product" label