【问题标题】:Add the product name to the email subject in WooCommerce将产品名称添加到 WooCommerce 中的电子邮件主题
【发布时间】:2019-04-15 10:08:13
【问题描述】:

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

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

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;
}

【问题讨论】:

    标签: php wordpress woocommerce hook-woocommerce email-notifications


    【解决方案1】:

    你没有使用正确的钩子并且有错误......而是使用以下内容:

    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 文件中。经过测试并且可以工作。

    注意:一个订单可以有很多商品,也可以有很多产品名称。在上面的代码中,我只保留了第一个产品名称……

    如果您想处理多个产品名称,您将使用:

    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 文件中。经过测试并且可以工作。


    相关:Add a custom placeholder to email subject in WooCommerce

    【讨论】:

    • 感谢帮助代码工作正常,上面附上我的代码现在也可以进行一些修改。
    猜你喜欢
    • 2020-12-15
    • 2020-10-03
    • 1970-01-01
    • 1970-01-01
    • 2021-07-22
    • 2014-04-24
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    相关资源
    最近更新 更多