有条件地基于自定义字段的Woocommerce电子邮件通知收件人
问题描述:
我有一个自定义字段的结帐表单。有条件地基于自定义字段的Woocommerce电子邮件通知收件人
我想根据自定义字段中的值为订单电子邮件添加额外的收件人。自定义字段当前是一个只有3个选项的下拉菜单。
下面是我能够拼凑一些谷歌搜索的代码,但这似乎并不奏效。
function sv_conditional_email_recipient($recipient, $order) {
$custom_field = get_post_meta($orderid, 'custom_field', true);
if ($custom_field == "Value 1")
{
$recipient .= ', [email protected]';
}
elseif ($custom_field == "Value 2")
{
$recipient .= ', [email protected]';
}
elseif ($custom_field == "Value 3")
{
$recipient .= ', [email protected]';
}
return $recipient;
}
add_filter('woocommerce_email_recipient_new_order', 'sv_conditional_email_recipient', 10, 2);
任何帮助表示赞赏。
谢谢。
答
你的问题来自未定义的$ orderid。试试这个:
add_filter('woocommerce_email_recipient_new_order', 'new_order_conditional_email_recipient', 10, 2);
function new_order_conditional_email_recipient($recipient, $order) {
// 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 .= ', [email protected]';
elseif ($custom_field == "Value 2")
$recipient .= ', [email protected]';
elseif ($custom_field == "Value 3")
$recipient .= ', [email protected]';
return $recipient;
}
代码放在您的活动子主题(或主题)的function.php文件或也以任何插件文件。
代码已经过测试,可用于WooCommerce 2.6.x和3+。