Why would wp_set_object_terms save only the first value from an array?
I'm using a frontend form to programmatically create a Woocommerce product. The frontend form uses a multiple select field that dynamically pulls in all product categories where each option value is the ID of each product category term. Here's the form:
<form action="" name="form" method="post" id="edit_image_form" enctype="multipart/form-data">
<div class="form-group">
<label>Category</label>
<select name="image_category[]" id="image_category" class="form-control select-checkbox" multiple />
<?php
$cat_args = array(
'orderby' => 'name',
'order' => 'asc',
'hide_empty' => false,
'exclude' => array(72) //Uncategorised
);
$product_categories = get_terms( 'product_cat', $cat_args );
if( !empty($product_categories) ){
foreach ($product_categories as $key => $category) {
echo '<option value="'.$category->term_id.'">'.$category->name.'</option>';
}
}
?>
</select>
</div>
<input type="submit" class="btn btn-primary" value="Add Product" name="submit" />
</form>
After selecting 3 terms from the select field, it spits out the following:
array(3) {
[0]=> string(3) "143"
[1]=> string(3) "145"
[2]=> string(3) "113"
}
Am I supposed to turn the above array into a comma separated string like below?
$image_category = implode( ", ", $_POST['image_category'] ); //Outputs 143, 145, 113
wp_set_object_terms($product->get_id(), array((int)$image_category), 'product_cat');
If so, it's only saving the first term (143). I can confirm the other three IDs definitely already exist. Where am I going wrong?