php - 根据 Woocommerce 中的运输方式向客户通知添加回复电子邮件
问题描述
例如,所有从默认 woocommerce 电子邮件地址发送给客户的订单电子邮件,webshop@shop.com
这没关系,但我想为这些电子邮件添加回复电子邮件地址,以便用户能够回复此自定义地址,例如reply@webshop.com
.
另外我想根据$order
送货方式设置这个回复电子邮件地址。如果送货方式是' local_pickup_plus
'设置回复地址reply1@webshop.com
,否则设置reply2@webshop.com
。
我发现,我可以使用woocommerce_email_headers
过滤器修改标题,但仅限于发送给管理员的电子邮件。
add_filter( 'woocommerce_email_headers', 'mycustom_headers_filter_function', 10, 3);
function mycustom_headers_filter_function( $headers, $object, $order ) {
if ($object == 'new_order') {
$headers .= 'Reply-to: teszt@teszt.hu';
}
return $headers;
}
如何为客户电子邮件设置此项?
解决方案
以下代码将允许您根据客户电子邮件通知的运输方式有条件地添加不同的回复电子邮件:
// Utility function to get the shipping method Id from order object
function wc_get_shipping_method_id( $order ){
foreach ( $order->get_shipping_methods() as $shipping_method ) {
return $shipping_method->get_method_id();
}
}
// Add coditionally a "reply to" based on shipping methods IDs for specific email notifications
add_filter( 'woocommerce_email_headers', 'add_headers_replay_to_conditionally', 10, 3 );
function add_headers_replay_to_conditionally( $headers, $email_id, $order ) {
// Avoiding errors
if ( ! is_a( $order, 'WC_Order' ) || ! isset( $email_id ) )
return $headers;
// The defined emails notifications to customer
$allowed_email_ids = array('customer_on_hold_order', 'customer_processing_order', 'customer_completed_order');
// Only for specific email notifications to the customer
if( in_array( $email_id, $allowed_email_ids ) ) {
// Local Pickup Plus shipping method
if( wc_get_shipping_method_id( $order ) === 'local_pickup_plus' ){
$headers .= "Reply-to: reply1@webshop.com". "\r\n"; // Email adress 1
}
// Other shipping methods
else {
$headers .= "Reply-to: reply2@webshop.com". "\r\n"; // Email adress 2
}
}
return $headers;
}
代码在您的活动子主题(活动主题)的 function.php 文件中。测试和工作。
推荐阅读
- excel - VBA 代码故障:应用程序。ScreenUpdating = False 在一个按钮下调用 subs 时不起作用
- javascript - 如果数组有多个 id,则创建新对象
- c# - C# - 列表中第一个插入对象的值更改
- c - 关于将代码闪存到 ESP32 板上
- javascript - 静态成员不能引用类类型参数.ts
- mysql - MySql从另一个表中添加带有case语句的额外列以选择查询
- arrays - 如何在 c 程序中使用混合类型制作二维数组?
- javascript - 谷歌分析页面视图发送但不事件
- windows - 在 Windows 中使用 Subfolder/*/FolderToDelete 删除所有文件夹的命令行?
- elixir - “找不到#PID<0.94.0> 的迁移运行程序进程”是什么意思以响应混合ecto.migrate?