基本上,在 woocommerce 中,您可以选择在 WooCommerce -> 设置 -> 电子邮件 -> 新订单中输入多个电子邮件地址(以逗号分隔),以将已完成的订单发送给谁。但我需要一种根据订购产品的客户的邮政编码仅发送给其中一个收件人的方法。或者完全覆盖 woocommerce 的处理方式。
我如何绑定(bind)负责此操作的函数,以便发送给正确的收件人?基本上,是否有为此定义的 Hook ,或者是否存在类似这样的插件,或者我必须编辑核心 WooCommerce 文件吗?如果需要对核心文件进行编辑,有人可以指出我哪些文件需要编辑的正确方向吗?
最佳答案
我对上面 helgatheviking 的答案遇到了一些麻烦,而且用例也略有不同。 我的问题/需求是:
- 我不太明白上面介绍的过滤器名称。
- class-wc-email.php 中的
public 函数 get_recipient()
需要一个字符串,但得到的是一个数组。 - A 还希望根据付款方式(而不是邮政编码)有条件地添加额外收件人。
这是我所做的:
- 添加了完整的过滤器名称,而不仅仅是后缀:
woocommerce_email_recipient_new_order
- 用字符串连接
$email .= ',' 替换了
。explode()
和array_push()
。 $additional_email; - 有条件地检查付款方式:
if( $order->get_ payment_method() == "cod")
。
完整示例:
add_filter( 'woocommerce_email_recipient_new_order' , 'so_26429482_add_recipient', 20, 2 );
function so_26429482_add_recipient( $email, $order ) {
// !empty($order) prevents a fatal error in WooCommerce Settings
// !empty($email) prevents the duplicate email from being sent in cases where the filter is run outside of when emails are normally sent. In my case, when using https://wordpress.org/plugins/woo-preview-emails/
if(!empty($order) && !empty($email)) {
$additional_email = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a1c8d2c7e1c5ceccc0c8cf8fc2ce" rel="noreferrer noopener nofollow">[email protected]</a>";
if( $order->get_payment_method() == "cod" ) {
$email .= ',' . $additional_email;
} else {
$email .= ',<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="87e6e9e8f3efe2f5c7e3e8eae6eee9a9e4e8" rel="noreferrer noopener nofollow">[email protected]</a>';
}
}
return $email;
}
关于php - Woocommerce - 需要根据邮政编码将电子邮件发送到特定地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26429482/