且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将产品名称添加到WooCommerce中的电子邮件主题

更新时间:2023-01-09 13:22:31

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

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中的电子邮件主题