在 WooCommerce 新订单电子邮件通知主题中添加产品名称

问题描述 投票:0回答:1

我希望更改发送给店主的包含产品名称的电子邮件的主题行。 我看到这段代码显示了客户的名字 我如何调整此代码以具有产品名称

/*
 * goes in theme functions.php or a custom plugin
 *
 * Subject filters: 
 *   woocommerce_email_subject_new_order
 *   woocommerce_email_subject_customer_processing_order
 *   woocommerce_email_subject_customer_completed_order
 *   woocommerce_email_subject_customer_invoice
 *   woocommerce_email_subject_customer_note
 *   woocommerce_email_subject_low_stock
 *   woocommerce_email_subject_no_stock
 *   woocommerce_email_subject_backorder
 *   woocommerce_email_subject_customer_new_account
 *   woocommerce_email_subject_customer_invoice_paid
 **/
add_filter('woocommerce_email_subject_new_order', 'change_admin_email_subject', 1, 2);

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

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

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

    return $subject;
}

也许我们只需要改变这里

$subject = sprintf( '[%s] New Customer Order (# %s) from Name %s %s', $blogname, $item->get_name, $order->billing_first_name, $order->billing_last_name );

    return $subject;
}
php woocommerce product orders email-notifications
1个回答
0
投票

您的实际代码确实已经过时......要将购买的产品名称(和数量)添加到发送给管理员的新订单电子邮件通知的主题,请使用以下命令:

add_filter('woocommerce_email_subject_new_order', 'change_email_subject_new_order', 10, 2);
function change_email_subject_new_order( $formatted_subject, $order ) {
    $products = array(); // Initializing

    // Loop through order items
    foreach( $order->get_items() as $item ){
        // Add formatted product name and quantity to the array
        $products[] = sprintf( '%s × %d', $item->get_name(), $item->get_quantity() );
    }

    $count    = count($products); // Products count
    $products = implode(', ', $products); // Convert the array to a string

    return sprintf( 
        __('New Customer Order (# %s), %s, from %s %s', 'woocommerce'),  
        products,
        $order->get_billing_first_name(), 
        $order->get_billing_last_name() 
    );
}

代码位于子主题的functions.php 文件中(或插件中)。已测试并有效。

© www.soinside.com 2019 - 2024. All rights reserved.