将产品名称添加到WooCommerce中的电子邮件主题 [英] Add the product name to the email subject in WooCommerce

查看:104
本文介绍了将产品名称添加到WooCommerce中的电子邮件主题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在此电子邮件的主题行中添加产品名称.是否有电子邮件主题行可用过滤器的列表?我希望能够做到这一点而不必编写自定义php.我正在寻找做这样的事情:

How can I add the product name in the subject line of this email. is there a list of the available filters for the email subject lines? I'd like to be able to do this without having to write custom php. I'm looking to do something like this:

已完成订单-[{product_name}]({order_number})-{order_date} 以下代码已尝试并添加到functions.php中,但未显示主题:

Completed Order - [{product_name}] ({order_number}) - {order_date} Below code tried and added in functions.php but subject is not displaying:

add_filter( 'woocommerce_email_subject_customer_completed_order', 'customer_completed_order_subject', 10, 2 ); 
function customer_completed_order_subject($string,$order){
$fname = $order->billing_first_name;
$lanme = $order->billing_last_name;
$email = $order->billing_email;
$items = $order->get_items();
foreach($items as $meta_key => $items){
    $product_name = $items['name'];
    $product_id = $items['product_id'];
}
$subject_line = get_field('email_subject',$product_id);
$subject_line = str_replace('{product}',$product_name,$subject_line);
$subject_line = str_replace('{biller_fname}',$fname,$subject_line);
$subject_line = str_replace('{biller_lastname}',$lanme,$subject_line);
$subject_line = str_replace('{biller_email}',$email,$subject_line);
$subject_line = str_replace('{blog_name}',get_bloginfo('name'),$subject_line);
return $subject_line;
}

推荐答案

您使用的钩子不正确,并且存在错误……请使用以下内容:

You are not using the correct hook and there are mistakes… Instead use the following:

add_filter( 'woocommerce_email_format_string' , 'add_custom_email_format_string', 10, 2 );
function add_custom_email_format_string( $string, $email ) {
    $order       = $email->object; // Get the instance of the WC_Order OBJECT
    $order_items = $order->get_items(); // Get Order items
    $order_item  = reset($order_items); // Get the irst order item

    // Replace placeholders with their respective values
    $string = str_replace( '{biller_fname}', $order->billing_first_name(), $string );
    $string = str_replace( '{biller_lname}', $order->billing_last_name(), $string );
    $string = str_replace( '{biller_email}', $order->billing_email(), $string );
    $string = str_replace( '{product_name}', $order_item->get_name(), $string );
    $string = str_replace( '{blog_name}', get_bloginfo('name'), $string );

    return $string; 
}

代码进入您的活动子主题(或活动主题)的function.php文件中.经过测试,可以正常工作.

Code goes in function.php file of your active child theme (or active theme). Tested and works.

注意:一个订单可以包含很多商品,所以有很多产品名称.在上面的代码中,我仅保留第一个产品名称...

Note: An order can have many items, so many product names. In the code above, I only keep the first product name…

如果要处理多个产品名称,请使用:

If you want to handle multiple product names, you will use:

add_filter( 'woocommerce_email_format_string' , 'add_custom_email_format_string', 10, 2 );
function add_custom_email_format_string( $string, $email ) {
    $order          = $email->object; // Get the instance of the WC_Order OBJECT
    $products_names = array();

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

    // Replace placeholders with their respective values
    $string = str_replace( '{biller_fname}', $order->billing_first_name(), $string );
    $string = str_replace( '{biller_lname}', $order->billing_last_name(), $string );
    $string = str_replace( '{biller_email}', $order->billing_email(), $string );
    $string = str_replace( '{product_name}', implode(' ', $products_names), $string );
    $string = str_replace( '{blog_name}', get_bloginfo('name'), $string );

    return $string;
}

代码进入您的活动子主题(或活动主题)的function.php文件中.经过测试,可以正常工作.

Code goes in function.php file of your active child theme (or active theme). Tested and works.

相关:向其中添加自定义占位符WooCommerce中的电子邮件主题

这篇关于将产品名称添加到WooCommerce中的电子邮件主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆