![](/img/trans.png)
[英]Add a multi-select field for featured Posts in WooCommerce Product Category settings
[英]WooCommerce Multi Select for Single Product Field
我正在嘗試在單個產品頁面上添加列表框,我想知道 woocommerce 中沒有多選選項,對於get_option
字段 woocommerce 支持多選但對於post_meta
woocommerce 僅支持單選,不確定 woocommerce 是否有任何限制,或者我可以錯過一些東西以獲得多選? 這是我嘗試過的以下代碼
function create_multiselect() {
woocommerce_wp_select(array(
'id' => 'newoptions',
'class' => 'newoptions',
'label' => __('Testing Multiple Select', 'woocommerce'),
'options' => array(
'1' => 'User1',
'2' => 'User2',
'3' => 'User3',
))
);
}
add_action("woocommerce_product_options_pricing","create_multiselect");
任何建議都會很棒。
woocommerce_wp_select()
函數不支持多選,您可以打開/woocommerce/includes/admin
目錄下的wc-meta-box-functions.php
文件查看默認行為。
但是是什么阻止您創建自己的函數,該函數將基於默認的 Woo 函數,並添加所需的功能,甚至更改默認函數(但仍然,修改插件文件不是最佳實踐)。 這是一個如何編寫具有多重支持的新函數的示例,與原始函數的唯一變化是添加了對名稱和多個屬性的支持,以及所選項目的不同邏輯(因為 post meta 現在是一個數組)。
function woocommerce_wp_select_multiple( $field ) {
global $thepostid, $post, $woocommerce;
$thepostid = empty( $thepostid ) ? $post->ID : $thepostid;
$field['class'] = isset( $field['class'] ) ? $field['class'] : 'select short';
$field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
$field['name'] = isset( $field['name'] ) ? $field['name'] : $field['id'];
$field['value'] = isset( $field['value'] ) ? $field['value'] : ( get_post_meta( $thepostid, $field['id'], true ) ? get_post_meta( $thepostid, $field['id'], true ) : array() );
echo '<p class="form-field ' . esc_attr( $field['id'] ) . '_field ' . esc_attr( $field['wrapper_class'] ) . '"><label for="' . esc_attr( $field['id'] ) . '">' . wp_kses_post( $field['label'] ) . '</label><select id="' . esc_attr( $field['id'] ) . '" name="' . esc_attr( $field['name'] ) . '" class="' . esc_attr( $field['class'] ) . '" multiple="multiple">';
foreach ( $field['options'] as $key => $value ) {
echo '<option value="' . esc_attr( $key ) . '" ' . ( in_array( $key, $field['value'] ) ? 'selected="selected"' : '' ) . '>' . esc_html( $value ) . '</option>';
}
echo '</select> ';
if ( ! empty( $field['description'] ) ) {
if ( isset( $field['desc_tip'] ) && false !== $field['desc_tip'] ) {
echo '<img class="help_tip" data-tip="' . esc_attr( $field['description'] ) . '" src="' . esc_url( WC()->plugin_url() ) . '/assets/images/help.png" height="16" width="16" />';
} else {
echo '<span class="description">' . wp_kses_post( $field['description'] ) . '</span>';
}
}
echo '</p>';
}
如何使用該功能的示例:
woocommerce_wp_select_multiple( array(
'id' => 'newoptions',
'name' => 'newoptions[]',
'class' => 'newoptions',
'label' => __('Testing Multiple Select', 'woocommerce'),
'options' => array(
'1' => 'User1',
'2' => 'User2',
'3' => 'User3',
))
);
沒有必要創建一個新函數。 woocommerce_wp_select
處理這個問題。 可用的屬性之一是custom_attributes
,它可以接受一個數組。 如果你傳入一個array('multiple'=>'multiple)
那么它會呈現一個多選。 為了序列化和處理輸入,您只需在 name 字段中提供一個name[]
,它就像魔術一樣工作。 這是一個例子——
woocommerce_wp_select(
array(
'id' => '_cb_days_available',
'name' => '_cb_days_available[]',
'label' => __('Days Offered', 'woocommerce'),
'description' => 'Which days of the week is this charter available?',
'default' => get_option('cb_open_days'),
'desc_tip' => 'true',
'class' => 'cb-admin-multiselect',
'options' => array(
'Mon' => 'Monday',
'Tue' => 'Tuesday',
'Wed' => 'Wednesday',
'Thu' => 'Thursday',
'Fri' => 'Friday',
'Sat' => 'Saturday',
'Sun' => 'Sunday'
),
'custom_attributes' => array('multiple' => 'multiple')
)
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.