admin管理员组

文章数量:1429841

I am trying to get the names of all the products ordered separated by a comma and added into the subject line of the new order email sent to the admin.

Here's some code I have but it's only adding in the first product name, not all of them:

add_filter('woocommerce_email_subject_new_order', 'change_admin_email_subject', 1, 2);

function change_admin_email_subject( $subject, $order ) {
global $woocommerce;

    $items = $order->get_items();
    foreach ( $items as $item ) {
        $product_name = $item->get_name();
    }

$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);

$subject = sprintf( '[%s] New Customer Order (#%s) of '.$product_name.' from %s %s', $blogname, $order->id, $order->billing_first_name, $order->billing_last_name);

return $subject;
}

I am trying to get the names of all the products ordered separated by a comma and added into the subject line of the new order email sent to the admin.

Here's some code I have but it's only adding in the first product name, not all of them:

add_filter('woocommerce_email_subject_new_order', 'change_admin_email_subject', 1, 2);

function change_admin_email_subject( $subject, $order ) {
global $woocommerce;

    $items = $order->get_items();
    foreach ( $items as $item ) {
        $product_name = $item->get_name();
    }

$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);

$subject = sprintf( '[%s] New Customer Order (#%s) of '.$product_name.' from %s %s', $blogname, $order->id, $order->billing_first_name, $order->billing_last_name);

return $subject;
}
Share Improve this question asked May 1, 2019 at 20:47 Nick TaylorNick Taylor 1379 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

Since Woocommerce 3 your actual code is a bit outdated. In the following you will get all products names (coma separated) in the email subject.

add_filter('woocommerce_email_subject_new_order', 'change_admin_email_subject', 10, 2);
function change_admin_email_subject( $subject, $order ) {
    $products_names = array();

    foreach ( $order->get_items() as $item ) {
        $products_names[] = $item->get_name();
    }

    return sprintf( '[%s] New Customer Order (#%s) of %s from %s %s', 
        wp_specialchars_decode(get_option('blogname'), ENT_QUOTES), 
        $order->get_id(), 
        implode(', ', $products_names),
        $order->get_billing_first_name(),  
        $order->get_billing_last_name()
    );
}

本文标签: phpAdd the Name of Products Ordered to Admin New Order Email WooCommerce