Hi, John Thank you for your feedback, I’m trying to implement select2 into Woocommerce I created a plugin that is a normal inputfield, where someone can add the Categories Manually. but I have changed that to be a dropdown (select field)with all the categories in the dropdown. now the dropdown works perfectly if you only select 1 Category, but I would like to select up to 3 different categories, that is why I decided to use the select 2 but my knowledge of select to is very limited even when I tried following the examples!
but let me see if I can get it all in order for you.
This script Calls for the Categories. as a select filed
<?php
$args = array( 'order' => 'ASC',
'hide_empty' => $hide_empty,
'include' => $ids,
'posts_per_page' =>'-1'
);
$product_categories = get_terms( 'product_cat', $args );
echo '';
foreach( $product_categories as $category ){
// echo "slug ) . "'>" . esc_html( $category->name ) . "";
echo "name ) . "'>" . esc_html( $category->name ) . "";
}
echo "
";
?>
the output is this:
< select class=“js-example-basic-multiple select2-hidden-accessible” name=“crosssell” multiple="" tabindex="-1" aria-hidden=“true”>
< option value=“3 In One Oil”>3 In One Oil< /option>
< option value=“Angle Grinder”>Angle Grinder< /option>
< option value=“Anti Spat Spray Silicone & Non Silicone”>Anti Spat Spray Silicone & Non Silicone< /option>
< option value=“Assorted Bolts & Nuts”>Assorted Bolts & Nuts< /option>
< option value=“Assorted Cable Clips”>Assorted Cable Clips< /option>
< option value=“Assorted Flat Washers”>Assorted Flat Washers< /option>
< option value=“Assorted Hex Nuts”>Assorted Hex Nuts< /option>
< option value=“Assorted Machine Screws”>Assorted Machine Screws< /option>
< /select>
the select field is a very long one so I shorten it a lot
Now I can get it to look like its working by selecting lets say 3 categories but it only post the last 1
but when I change it back to input field text and type the categories is separated by a comma it works
PHP script for that is
if ( isset( $_POST[‘crosssell’] ) && in_array($taxonomy, $pages)) {
update_term_meta( $term_id, ‘crosssell’, sanitize_text_field($_POST[‘crosssell’]) );
}
the when I click edit to remove selected it gives me the error as above [object Object] if you need more info please let me know