WooCommerce自定义字段-多选 [英] WooCommerce Custom Fields - Multiselect
问题描述
我在WooCommerce的结帐页面中添加了额外的字段,我可以添加诸如文本框之类的基本字段,但是需要添加一个(多个)选择框,用户可以在其中选择多个项目.我已经弄清楚了如何通过代码添加选择框,如下所示:
I'm adding extra fields to the checkout page in WooCommerce, I can add basic fields like a text box, but need to add a (multi) select box, where the user can choose multiple items. I've figured out how to add a select box through code, like this:
add_action('woocommerce_after_order_notes', 'my_custom_checkout_field');
function my_custom_checkout_field( $checkout ) {
echo '<div id="my_custom_checkout_field"><h3>'.__('My Field').'</h3>';
woocommerce_form_field( 'my_field_name', array(
'type' => 'select',
'class' => array('my-field-class form-row-wide'),
'label' => __('Fill in this field'),
'placeholder' => __('Enter something'),
'options' => array(
'Buick' => __('Buick', 'woocommerce' ),
'Ford' => __('Ford', 'woocommerce' )
)
), $checkout->get_value( 'my_field_name' ));
echo '</div>';
}
但这只是一个选择下拉菜单.
我可以对多项选择执行类似操作吗?
还是您有推荐的WooCommerce扩展程序?
But that's just a single select drop down.
Can I do something similar for a multi-select?
Or do you have a WooCommerce extension that'd you recommend?
请指教,在此先感谢!
Please advice, thanks in advance!
推荐答案
您需要创建自己的自定义字段类型处理程序.如果您查看 WooCommerce源代码,您会发现您可以使用过滤器:'woocommerce_form_field_' . $args['type']
You need to create your own custom field type handler. If you look at WooCommerce source code you will see that you can use filter: 'woocommerce_form_field_' . $args['type']
我还没有真正测试过,这只是从单个选择"控件中稍作改动的代码,但是您明白了:
I haven't really tested this, this is just slightly altered code from single "select" control, but you get the point:
add_filter( 'woocommerce_form_field_multiselect', 'custom_multiselect_handler', 10, 4 );
function custom_multiselect_handler( $field, $key, $args, $value ) {
$options = '';
if ( ! empty( $args['options'] ) ) {
foreach ( $args['options'] as $option_key => $option_text ) {
$options .= '<option value="' . $option_key . '" '. selected( $value, $option_key, false ) . '>' . $option_text .'</option>';
}
$field = '<p class="form-row ' . implode( ' ', $args['class'] ) .'" id="' . $key . '_field">
<label for="' . $key . '" class="' . implode( ' ', $args['label_class'] ) .'">' . $args['label']. $required . '</label>
<select name="' . $key . '" id="' . $key . '" class="select" multiple="multiple">
' . $options . '
</select>
</p>' . $after;
}
return $field;
}
然后在您的代码中将类型声明为'multiselect':
And in your code just state the type as 'multiselect':
add_action('woocommerce_after_order_notes', 'my_custom_checkout_field');
function my_custom_checkout_field( $checkout ) {
echo '<div id="my_custom_checkout_field"><h3>'.__('My Field').'</h3>';
woocommerce_form_field( 'my_field_name', array(
'type' => 'multiselect',
'class' => array('my-field-class form-row-wide'),
'label' => __('Fill in this field'),
'placeholder' => __('Enter something'),
'options' => array(
'Buick' => __('Buick', 'woocommerce' ),
'Ford' => __('Ford', 'woocommerce' )
)
), $checkout->get_value( 'my_field_name' ));
echo '</div>';
}
这篇关于WooCommerce自定义字段-多选的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!