在WooCommerce新订单电子邮件通知的主题中添加产品名称
P粉990008428
P粉990008428 2023-08-18 08:55:45
[PHP讨论组]

我想要修改发送给店主的电子邮件的主题行,将产品名称放入其中。 我看到了这段代码,可以将客户的名字放在前面 我该如何调整这段代码来包含产品名称

/*
 * 放在主题的functions.php或自定义插件中
 *
 * 主题过滤器: 
 *   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] 新客户订单 (# %s) 来自姓名 %s %s', $blogname, $order->id, $order->billing_first_name, $order->billing_last_name );

    return $subject;
}

也许我们只需要在这里进行修改

$subject = sprintf( '[%s] 新客户订单 (# %s) 来自姓名 %s %s', $blogname, $item->get_name, $order->billing_first_name, $order->billing_last_name );

    return $subject;
}


P粉990008428
P粉990008428

全部回复(1)
P粉207483087

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

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(); // 初始化

    // 循环遍历订单项目
    foreach( $order->get_items() as $item ){
        // 将格式化的产品名称和数量添加到数组中
        $products[] = sprintf( '%s × %d', $item->get_name(), $item->get_quantity() );
    }

    $count    = count($products); // 产品数量
    $products = implode(', ', $products); // 将数组转换为字符串

    return sprintf( 
        __('[%s] 新客户订单(#%s),%s,来自%s%s', 'woocommerce'), 
        wp_specialchars_decode(get_option('blogname'), ENT_QUOTES), $order->get_order_number(), 
        sprintf( _n('产品(%s)', '产品(%s)', $count, 'woocommerce'), $products, $products ),
        $order->get_billing_first_name(), $order->get_billing_last_name() 
    );
}

将代码放在您的子主题的functions.php文件中(或插件中)。经过测试,可以正常工作。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号