php - 如何将多个复选框添加到 Woocommerce 结帐页面并允许选择有条件地向收件人发送电子邮件?
问题描述
我将不胜感激以下任何帮助...
在我的 WooCommerce 结帐页面上,我需要允许我的客户选择多个分销商,然后将新订单电子邮件发送给我自己,并且只发送给他们选择的分销商。
我一生都想不出如何使用多复选框进行设置,这将允许他们选择多个(即他们选择分销商 1 和 3,只有我们三个人会收到新的订单电子邮件)。
我能够使用下拉菜单来解决这个问题(感谢阅读了这个社区中的各种帖子),但是,这个选项只允许一个选择。任何帮助将不胜感激!:)
我能够拼凑的当前代码:
// // Add custom checkout field
add_action( 'woocommerce_after_order_notes', 'sj_custom_checkout_field' );
function sj_custom_checkout_field( $checkout ) {
echo '<div id="sj_custom_checkout_field"><h2>' . __('Distributor') . '</h2>';
woocommerce_form_field( 'my_field_name', array(
'type' => 'select',
'class' => array('wps-drop'),
'required' => true, // Missing
'options' => array(
'' => __( 'Select Your Preferred Distributor', 'wps' ),
'Distributor 1' => __( 'Distributor 1', 'wps' ),
'Distributor 2' => __( 'Distributor 2', 'wps' ),
'Distributor 3' => __( 'Distributor 3', 'wps' ),
'Distributor 4' => __( 'Distributor 4', 'wps' ),
'Distributor 5' => __( 'Distributor 5', 'wps' )
)
), $checkout->get_value( 'my_field_name' ) );
echo '</div>';
}
// Process the checkout
add_action('woocommerce_checkout_process', 'sj_custom_checkout_field_process');
function sj_custom_checkout_field_process() {
// Check if set, if its not set add an error.
if ( empty( $_POST['my_field_name'] ) )
wc_add_notice( __( 'Please select your preferred distributor.' ), 'error' );
}
// Save the custom checkout field in the order meta
add_action( 'woocommerce_checkout_update_order_meta', 'sj_custom_field_checkout_update_order_meta', 10, 1 );
function sj_custom_field_checkout_update_order_meta( $order_id ) {
if ( ! empty( $_POST['my_field_name'] ) )
update_post_meta( $order_id, 'my_field_name', $_POST['my_field_name'] );
}
add_filter( 'woocommerce_email_recipient_new_order', 'new_order_conditional_email_recipient', 10, 2 );
function new_order_conditional_email_recipient( $recipient, $order ) {
if( is_admin() ) return $recipient;
// Get the order ID (Woocommerce retro compatibility)
$order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;
// Get the custom field value (with the right $order_id)
$my_field_name = get_post_meta( $order_id, 'my_field_name', true );
if ($my_field_name == "Distributor 1")
$recipient .= ',1@email.com';
elseif ($my_field_name == "Distributor 2")
$recipient .= ',2@email.com';
elseif ($my_field_name == "Distributor 3")
$recipient .= ',3@email.com';
elseif ($my_field_name == "Distributor 4")
$recipient .= ',4@email.com';
elseif ($my_field_name == "Distributor 5")
$recipient .= ',5@email.com';
return $recipient;
}
解决方案
推荐阅读
- angular - 如何在 mat-form-field 中使用字体真棒图标在 mat-label 上显示 matTooltip
- arduino - 如何计算 Arduino 中的移动平均线?
- perl - yml 文件合并为一个
- android - 颤振格式日期时间
- php - 通过 PHP 缓存页面
- java - gitlab 多个缓存 - 不考虑全局缓存
- python - 试图在以太坊上获得待处理的交易。它显示错误。为什么?
- ms-word - 从 Dynamics CRM 在 Word 中创建动态表
- java - 如何在java中对数组json应用缩进
- python - 如何将geojson坐标转换为python中的英制坐标?