dongqiaogouk86049 2014-04-25 07:57
浏览 13
已采纳

单个产品领域的WooCommerce Multi Select

I am trying to add the list box on the single product page, I was wondering no option for multiselect in woocommerce, For get_option fields woocommerce supporting multiselect but for post_meta woocommerce support only single select, Not sure is there any limitation in woocommerce or i could miss something to get multiselect? Here is the below code i tried

 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");

Any Suggestion would be great.

  • 写回答

1条回答 默认 最新

  • douxu2467 2014-04-26 15:13
    关注

    woocommerce_wp_select() function does not support multiselect, you can open the wc-meta-box-functions.php file in /woocommerce/includes/admin directory to see the default behavior.

    But what's stopping you to create your own function that will be based on the default Woo function, and add the required features, or even change the default function ( but still, modifying the plugin files is not the best practice ). Here's an example of how to write a new function with multiple support, the only changes from original are added support for name and multiple attributes, and different logic for the selected items ( since post meta is now an array ).

    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>';
    }
    

    Example of how to use the function:

    woocommerce_wp_select_multiple( array(
        'id' => 'newoptions',
        'name' => 'newoptions[]',
        'class' => 'newoptions',
        'label' => __('Testing Multiple Select', 'woocommerce'),
        'options' => array(
            '1' => 'User1',
            '2' => 'User2',
            '3' => 'User3',
        ))
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置