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.
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 thecustom_attributes
which can accept an array. If you pass in anarray('multiple'=>'multiple)
then it renders a multiselect. In order to serialize and handle the input, you just provide aname[]
in the name field and it works like magic. Here is an example –woocommerce_wp_select()
function does not support multiselect, you can open thewc-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 ).
Example of how to use the function: