Чтобы определить только для уведомления по электронной почте администратора WooCommerce КРЕДИТЫ: @ Loictheazte c
// Setting the "sent_to_admin" as a global variable
function email_order_id_as_a_global($order, $sent_to_admin, $plain_text, $email) {
$GLOBALS['email_data'] = array(
'sent_to_admin' => $sent_to_admin, // <== HERE we set "$sent_to_admin" value
'email_id' => $email->id, // The email ID (to target specific email notification)
);
}
add_action('woocommerce_email_before_order_table', 'email_order_id_as_a_global', 1, 4);
function woocommerce_before_order_add_cat( $name, $item ) {
// On email notifications
if ( ! is_wc_endpoint_url() && $item->is_type('line_item') ) {
// Getting the custom 'email_data' global variable
$refNameGlobalsVar = $GLOBALS;
$email_data = $refNameGlobalsVar['email_data'];
// Only for admin email notifications
if( is_array( $email_data ) && $email_data['sent_to_admin'] ) {
// Get product id
$product_id = $item['product_id'];
// Tax
$tax = 'product_cat';
// Get post terms
$terms = wp_get_post_terms( $product_id, $tax, ['fields' => 'names'] );
if( $terms && ! is_wp_error( $terms )) {
$taxonomy = get_taxonomy($tax);
$name .= '<label>' . $taxonomy->label . ': </label>' . implode( ', ', $terms );
}
}
}
return $name;
}
add_filter( 'woocommerce_order_item_name', 'woocommerce_before_order_add_cat', 10, 2 );
Вы можете также используйте
woocommerce_order_item_meta_start
- или при желании вы также можете использовать
woocommerce_order_item_meta_end
вместо woocommerce_order_item_name
function add_product_categories_to_mail( $item_id, $item, $order, $plain_text ) {
// On email notifications
if ( ! is_wc_endpoint_url() && $item->is_type('line_item') ) {
// Getting the custom 'email_data' global variable
$refNameGlobalsVar = $GLOBALS;
$email_data = $refNameGlobalsVar['email_data'];
// Only for admin email notifications
if( is_array( $email_data ) && $email_data['sent_to_admin'] ) {
// Get product id
$product_id = $item['product_id'];
// Tax
$tax = 'product_cat';
// Get post terms
$terms = wp_get_post_terms( $product_id, $tax, ['fields' => 'names'] );
// Set variable
$name = '';
if( $terms && ! is_wp_error( $terms )) {
$taxonomy = get_taxonomy($tax);
$name = '<label>' . $taxonomy->label . ': </label>' . implode( ', ', $terms );
}
echo $name;
}
}
}
add_action( 'woocommerce_order_item_meta_start', 'add_product_categories_to_mail', 10, 4 );
//add_action( 'woocommerce_order_item_meta_end', 'add_product_categories_to_mail', 10, 4 );