Custom Admin Email Subject for Woocommerce (3.8.0) Orders

I would like the Email Subject line for the admin email to change based on the product category.I've looked at ALL

I would like the Email Subject line for the admin email to change based on the product category. I've looked at ALL similar stackoverflow questions and NONE of them work for woocommerece 3.8.0 (see this and this).

What I have is this

function custom_admin_email_subject( $subject, $order ) {
    global $woocommerce;
    foreach($order->get_items() as $item_id => $item ){
        if ( has_term( 'Category 1 Name', 'product_cat' , $item->get_product_id() ) ) { 
            break;
            $subject = sprintf( 'Category 1 Email Subject Line' );
        }
    } 
    return $subject;
}

My code simply returns the default email subject line for new orders (which is set in woocommerce/settings/email). I can't figure out why my function does not recognize category names.

Can anyone tell me what is wrong with my code?

I am placing this code in my child-theme/functions.php file I am running woocommerce 3.8.0 and wordpress 5.3

I would like the Email Subject line for the admin email to change based on the product category. I've looked at ALL similar stackoverflow questions and NONE of them work for woocommerece 3.8.0 (see this and this).

What I have is this

function custom_admin_email_subject( $subject, $order ) {
    global $woocommerce;
    foreach($order->get_items() as $item_id => $item ){
        if ( has_term( 'Category 1 Name', 'product_cat' , $item->get_product_id() ) ) { 
            break;
            $subject = sprintf( 'Category 1 Email Subject Line' );
        }
    } 
    return $subject;
}

My code simply returns the default email subject line for new orders (which is set in woocommerce/settings/email). I can't figure out why my function does not recognize category names.

Can anyone tell me what is wrong with my code?

I am placing this code in my child-theme/functions.php file I am running woocommerce 3.8.0 and wordpress 5.3

Share Improve this question edited Nov 19, 2019 at 22:01 fuxia 107k39 gold badges255 silver badges459 bronze badges asked Nov 19, 2019 at 20:14 david leedavid lee 1314 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

The break is in the wrong place. When you put a break; in a loop (foreach), it quits the loop right at that point. You have your break before you set the $subject value so you're quitting the loop before the $subject is set. Set the subject, then break.

Also, your code snippet is missing the add_filter() call that triggers this filter function. This needs to be hooked to woocommerce_email_subject_new_order, the same as in the examples you linked to.

add_filter('woocommerce_email_subject_new_order', 'custom_admin_email_subject', 1, 2);    
function custom_admin_email_subject( $subject, $order ) {
    global $woocommerce;
    foreach($order->get_items() as $item_id => $item ){
        if ( has_term( 'Category 1 Name', 'product_cat' , $item->get_product_id() ) ) { 
            $subject = sprintf( 'Category 1 Email Subject Line' );
            break;
        }
    } 
    return $subject;
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744967059a4603745.html

相关推荐

  • Custom Admin Email Subject for Woocommerce (3.8.0) Orders

    I would like the Email Subject line for the admin email to change based on the product category.I've looked at ALL

    1天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信