How to add a custom text 'per item' based on product category on all WooCommerce emails?
Therefore I tried to modify Add a custom text for a particular product on specific email notification in Woocommerce and Display a custom text based on product category in WooCommerce Email answer code to my needs.
// Setting the email_is 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;
}
// Displaying product description in new email notifications
add_action( 'woocommerce_order_item_meta_end', 'product_description_in_new_email_notification', 10, 3 );
function product_description_in_new_email_notification( $product_cat, $cat, $order = null ){
// HERE define your targetted product CAT
$targeted_id = 'x';
// HERE define the text information to be displayed for the targeted product id
$text_information = __("There is an offer in this particular item", "woocommerce");
// Getting the email ID global variable
$refNameGlobalsVar = $GLOBALS;
$email_id = $refNameGlobalsVar['email_id_str'];
// If empty email ID we exit
if(empty($email_id)) return;
// Only for "New Order email notification" for your targeted product ID
if ( 'customer_completed_order' == $email_id && 'new_order' == $email_id
in_array( $targeted_id, array( $cat->get_product_cat(), $item->get_variation_id() ) ) ) {
// Display the text
echo '<div style="margin-top:10px"><p>' . $text_information . '</p></div>';
}
}
Unfortunately without the desired result. I would like to display custom text under an order item name for all products of a particular product category in all email notifications (this is per item, not per email). Any advice?
CodePudding user response:
Some notes on your code attempt:
$product_cat
and$cat
do not exist as arguments for thewoocommerce_order_item_meta_end
hook$cat->get_product_cat()
does not exist and is incorrect- Use
has_term()
WordPress function to check if the current post has any of given terms
So you get:
// Setting global variable
function action_woocommerce_email_before_order_table( $order, $sent_to_admin, $plain_text, $email ) {
$GLOBALS['email_data'] = array(
'email_id' => $email->id, // The email ID (to target specific email notification)
'is_email' => true // When it concerns a WooCommerce email notification
);
}
add_action( 'woocommerce_email_before_order_table', 'action_woocommerce_email_before_order_table', 1, 4 );
// Displaying description
function action_woocommerce_order_item_meta_end( $item_id, $item, $order, $plain_text ) {
// Getting the custom 'email_data' global variable
$ref_name_globals_var = $GLOBALS;
// Isset & NOT empty
if ( isset ( $ref_name_globals_var ) && ! empty( $ref_name_globals_var ) ) {
// Isset
$email_data = isset( $ref_name_globals_var['email_data'] ) ? $ref_name_globals_var['email_data'] : '';
// NOT empty
if ( ! empty( $email_data ) ) {
// Specific categories: the term name/term_id/slug. Several could be added, separated by a comma
$categories = array( 108, 1, 'categorie-1' );
// The text information
$text_information = __( 'There is an offer in this particular item', 'woocommerce' );
// Specific email notifications: multiple statuses can be added, separated by a comma
$email_ids = array( 'new_order', 'customer_processing_order', 'customer_completed_order', 'customer_on_hold_order' );
// Targeting specific email notifications AND check if the current post has any of given terms
if ( in_array( $email_data['email_id'], $email_ids ) && has_term( $categories, 'product_cat', $item->get_product_id() ) ) {
// Display the text
echo '<p>' . $text_information . '</p>';
}
}
}
}
add_action( 'woocommerce_order_item_meta_end', 'action_woocommerce_order_item_meta_end', 10, 4 );