Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WooCommerce Multi Select for Single Product Field

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.

like image 967
Vignesh Pichamani Avatar asked Sep 05 '25 03:09

Vignesh Pichamani


1 Answers

It is not necessary to create a new function. The woocommerce_wp_select handles this out of the box. One of the attributes available is the custom_attributes which can accept an array. If you pass in an array('multiple'=>'multiple) then it renders a multiselect. In order to serialize and handle the input, you just provide a name[] in the name field and it works like magic. Here is an example -

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')
    )
);
like image 50
MegPhillips91 Avatar answered Sep 08 '25 12:09

MegPhillips91