您可以通过两种方式做到这一点:
1) 启用<optgroup>
在 Woocommerce 中选择表单字段,您可以从 GitHub 使用:
2)启用<optgoup>
手动在选择字段中,例如:
add_action('woocommerce_before_order_notes', 'custom_checkout_select_field_with_optgroup', 10, 1 );
function custom_checkout_select_field_with_optgroup( $checkout ) {
$domain = 'woocommerce';
$title = __("Region", $domain);
$slug = sanitize_title($title);
$default = __("Select your region", $domain);
$value = $checkout->get_value($slug);
// Region option data array with optgroup
$options = array(
__("North Region", $domain) => array(
'region1' => __("Region 1", $domain),
'region2' => __("Region 2", $domain),
),
__("South Region", $domain) => array(
'region3' => __("Region 3", $domain),
'region4' => __("Region 4", $domain),
)
);
// The field
echo '<p class="form-row form-row-wide '.$slug.'-dropdown" id="'.$slug.'_field" data-priority="">
<label for="'.$slug.'" class="">'.$title.'</label>
<span class="woocommerce-input-wrapper">
<select name="'.$slug.'" id="'.$slug.'" class="select " data-placeholder="" autocomplete="'.$slug.'">
<option value="">'.$default.'</option>';
// Loop through "optgroup"
foreach( $options as $optgroup_label => $optgroup_options ) {
echo '<optgroup label="'.$optgroup_label.'">';
// Loop through "options" in the "optgroup"
foreach( $optgroup_options as $key => $label ) {
$selected = $value === $key ? ' selected="selected"': '';
echo '<option value="'.$key.'"'.$selected.'>'.$label.'</option>';
}
echo '</optgroup>';
}
echo '</select></span></p>';
}
代码位于活动子主题(或活动主题)的functions.php 文件中。经过测试并有效。
相关主题:在 Woocommerce Checkout 中的帐单国家/地区下方添加自定义字段