基于WooCommerce中的自定义字段值的电子邮件收件人
问题描述:
我已将自定义字段添加到我的Woocommerce商店的结帐页面。该字段是“餐馆位置”。在客户下订单时,我的目标是使用“餐馆位置”字段确定将订单确认发送至哪个电子邮件。基于WooCommerce中的自定义字段值的电子邮件收件人
下面是我如何定义自定义字段。
/////// Hook custom field in ///////
add_filter('woocommerce_checkout_fields', 'custom_checkout_fields');
function custom_checkout_fields($fields) {
$fields['order']['restaurant_location'] = array(
'label' => __('Food options', 'woocommerce'),
'placeholder' => _x('', 'placeholder', 'woocommerce'),
'required' => true,
'clear' => false,
'type' => 'select',
'options' => array(
'no' => __('New Orleans', 'woocommerce'),
'br' => __('Baton Rouge', 'woocommerce')
)
);
return $fields;
}
这是我对电子邮件过滤器的尝试。
add_filter('woocommerce_email_recipient_new_order', 'gon_conditional_email_recipient', 10, 2);
function gon_conditional_email_recipient($recipient, $order) {
$gon_order_data = $order->get_data();
$gon_restaurant_location = $gon_order_data['order']['restaurant_location'];
if ( $gon_restaurant_location == 'New Orleans') {
$recipient = '[email protected]';
return $recipient;
}
else if ( $gon_restaurant_location == 'Baton Rouge') {
$recipient = '[email protected]';
return $recipient;
}
return $recipient;
}
电子邮件过滤器工作,即我可以得到一个电子邮件要不要去的地址,但我似乎不能在“$gon_restaurant_location
”变量来拉正确。有任何想法吗?
感谢,
PS
答
您的自定义字段值不保存在数据库中,所以这就是为什么不工作。试试这个完整的解决方案,而不是:
// Add the custom checkout field
add_filter('woocommerce_after_order_notes', 'restaurant_location_checkout_field');
function restaurant_location_checkout_field($checkout) {
woocommerce_form_field('restaurant_location', array(
'type' => 'select',
'class' => array('my-field-class form-row-wide'),
'label' => __('Food options', 'woocommerce'),
'required' => true,
'options' => array(
'' => __('Please select an option', 'woocommerce'),
'New Orleans' => __('New Orleans', 'woocommerce'),
'Baton Rouge' => __('Baton Rouge', 'woocommerce')
)
), $checkout->get_value('restaurant_location'));
}
// Process the checkout (checking)
add_action('woocommerce_checkout_process', 'restaurant_location_field_process');
function restaurant_location_field_process() {
// Check if set, if its not set add an error.
if (! $_POST['restaurant_location'])
wc_add_notice(__('Please select a food option .'), 'error');
}
// Update the order meta with field value
add_action('woocommerce_checkout_update_order_meta', 'restaurant_location_field_update_order_meta');
function restaurant_location_field_update_order_meta($order_id) {
if (! empty($_POST['restaurant_location'])) {
update_post_meta($order_id, '_restaurant_location', sanitize_text_field($_POST['restaurant_location']));
}
}
// Display field value on the order edit page
add_action('woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1);
function my_custom_checkout_field_display_admin_order_meta($order){
echo '<p><strong>'.__('Food options', 'woocommerce').':</strong> ' . get_post_meta($order->get_id(), '_restaurant_location', true) . '</p>';
}
// Conditional Email recipient filter based on restaurant location
add_filter('woocommerce_email_recipient_new_order', 'conditional_email_recipient', 10, 2);
function conditional_email_recipient($recipient, $order) {
$location = get_post_meta($order->get_id(), '_restaurant_location', true);
$recipient = $location == 'New Orleans' ? ',[email protected]' : ',[email protected]';
return $recipient;
}
代码放在您的活动子主题(或主题)的function.php文件或也以任何插件文件。
此代码对Woocommerce 3+测试和工程
根据官方的开发者文档:Adding a custom special field
我要问,如果OP是*节省*的信息的任何地方。尽管我会建议切换到'woocommerce_checkout_create_order'以与其CRUD方法向前兼容。 – helgatheviking
@helgatheviking良好的建议+1 ... WooCommerce员工应该更新他们的结帐字段文档...即使有一些其他错误,如'get_post_meta($ order-> id'on)。 – LoicTheAztec