Mujtaba K
Mujtaba K

Reputation: 57

Hide price items for specific product categories in Woocommerce email notification

I'm using WooCommerce 3.1.1 and I am trying to replace the "price amount" with some text for specific product categories in New order noification for customers and admin.

I have almost tried everything but I am unable to locate the order item detail table for email notifications.

This email looks like this for now:

Email

Any help would be really appreciated.

Upvotes: 2

Views: 1184

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253919

You will need first to read this official documentation, to learn about Overriding WooCommerce Templates via your active Theme

The templates that you need to change and override is emails/email-order-items.php

At line 58 for your WC version (Or line 55 in WC version 3.2+), you will replace:

<td class="td" style="text-align:<?php echo $text_align; ?>; vertical-align:middle; border: 1px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;"><?php echo $order->get_formatted_line_subtotal( $item ); ?></td>

By this (where you should set your own category and replacement text string):

<?php
## ---- Variables to define (below)---- ##

$categories = array( 'clothing' ); // The Product categories coma separated (IDs slugs or names)
$replacement_text = __( 'Replacement text (here)' ); // The replacement text

// Getting the email ID global variable (From our function below)
$refNameGlobalsVar = $GLOBALS;
$email_id = $refNameGlobalsVar['email_id_str'];

// When matching product categories, "New Order", "Processing" and "On Hold" email notifications
if( has_term( $categories, 'product_cat', $product->get_id() )
&& ( $email_id == 'new_order' || $email_id == 'customer_processing_order' || $email_id == 'customer_on_hold_order' ) )
    $formated_line_subtotal = $replacement_text;
else
    $formated_line_subtotal = $order->get_formatted_line_subtotal( $item );
?>
<td class="td" style="text-align:<?php echo $text_align; ?>; vertical-align:middle; border: 1px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;"><?php echo $formated_line_subtotal; ?></td>

To Get the email ID you will need to add this in function.php file of your active child theme (or active theme):

// Setting the email_id as a global variable
add_action('woocommerce_email_before_order_table', 'the_email_id_as_a_global', 1, 4);
function the_email_id_as_a_global($order, $sent_to_admin, $plain_text, $email){
    $GLOBALS['email_id_str'] = $email->id;
}

Now you will get this when product category matches and for "New Order" (admin), "Customer On Hold Order" and "Customer Processing Order" email notifications only:

enter image description here

Upvotes: 2

Related Questions