基于自定义字段有条件地 Woocommerce 电子邮件通知收件人 [英] Woocommerce email notification recipient conditionally based on custom field
问题描述
我有一个带有自定义字段的结帐表单.
I have a checkout form with a custom field.
我想根据自定义字段中的值向订单电子邮件添加一个额外的收件人.自定义字段目前是一个只有 3 个选项的下拉菜单.
I would like to add an extra recipient to an order email based on the value in the custom field. The custom field is currently a drop down menu with only 3 options.
下面是我通过一些谷歌搜索拼凑起来的代码,但这似乎不起作用.
Below is the code I was able to piece together with some googling however this does not appear to work.
function sv_conditional_email_recipient( $recipient, $order ) {
$custom_field = get_post_meta($orderid, 'custom_field', true);
if ($custom_field == "Value 1")
{
$recipient .= ', email1@gmail.com';
}
elseif ($custom_field == "Value 2")
{
$recipient .= ', email2@gmail.com';
}
elseif ($custom_field == "Value 3")
{
$recipient .= ', email3@gmail.com';
}
return $recipient;
}
add_filter( 'woocommerce_email_recipient_new_order', 'sv_conditional_email_recipient', 10, 2 );
感谢任何帮助.
谢谢.
推荐答案
您的问题来自未定义的 $orderid
.试试这个:
Your problem comes from the $orderid
that is not defined. Try this instead:
add_filter( 'woocommerce_email_recipient_new_order', 'new_order_conditional_email_recipient', 10, 2 );
function new_order_conditional_email_recipient( $recipient, $order ) {
if ( ! is_a( $order, 'WC_Order' ) ) return $recipient; // (Optional)
// Get the order ID (retro compatible)
$order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;
// Get the custom field value (with the right $order_id)
$custom_field = get_post_meta( $order_id, 'custom_field', true );
if ($custom_field == "Value 1")
$recipient .= ', email1@gmail.com';
elseif ($custom_field == "Value 2")
$recipient .= ', email2@gmail.com';
elseif ($custom_field == "Value 3")
$recipient .= ', email3@gmail.com';
return $recipient;
}
代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中.
代码经过测试,可在 WooCommerce 2.6.x 和 3+ 上运行.
Code is tested and works on WooCommerce 2.6.x and 3+.
此挂钩仅针对new_order"电子邮件通知
This hook is targeting "new_order" email notification only
这篇关于基于自定义字段有条件地 Woocommerce 电子邮件通知收件人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!