【发布时间】:2026-01-21 02:15:02
【问题描述】:
我将不胜感激以下任何帮助...
在我的 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;
}
【问题讨论】:
-
你最好使用 type="radio" 然后使用 css 让它看起来像复选框。
标签: php wordpress woocommerce