Last active
November 2, 2021 16:32
-
-
Save NickGreen/26d66c181bce51e4e4074bac0704737f to your computer and use it in GitHub Desktop.
Add Product Category to email subject WooCommerce
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( 'woocommerce_email_subject_new_order', 'customizing_new_order_subject', 10, 2 ); | |
function customizing_new_order_subject( $formated_subject, $order ) { | |
$email = WC()->mailer->get_emails()['WC_Email_New_Order']; | |
$subject = $email->get_option( 'subject', $email->get_default_subject() ); | |
$product_categories = array(); | |
foreach ( $order->get_items() as $item ) { | |
$product_categories[] = implode( ', ', wp_get_post_terms( $item->get_product_id(), 'product_cat', array( 'fields' => 'names' ) ) ); | |
} | |
$categories_string = implode( ', ', $product_categories ); | |
$subject = str_replace( '{product_category}', $categories_string, $subject ); | |
return $email->format_string( $subject ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment