Сделаем метки товара вместо выпадающего списка чекбоксами в редакторе товара Woocommerce.

//в редакторе товара метки продуктов чекбоксами
add_action('add_meta_boxes', function () {
remove_meta_box('tagsdiv-product_tag', 'product', 'side');
add_meta_box(
'ws_product_tag_checklist',
'Метки товара',
'ws_render_product_tag_wrap_metabox',
'product',
'side',
'default'
);
}, 20);
function ws_render_product_tag_wrap_metabox($post) {
wp_nonce_field('ws_save_product_tag_checklist', 'ws_product_tag_checklist_nonce');
$taxonomy = 'product_tag';
$terms = get_terms([
'taxonomy' => $taxonomy,
'hide_empty' => false,
'orderby' => 'name',
'order' => 'ASC',
'number' => 500,
]);
if (is_wp_error($terms) || empty($terms)) {
echo '<p>Метки не найдены</p>';
return;
}
$selected_ids = wp_get_object_terms($post->ID, $taxonomy, ['fields' => 'ids']);
if (is_wp_error($selected_ids)) $selected_ids = [];
$selected_ids = array_map('intval', $selected_ids);
echo '
<style>
#ws-product-tag-wrap {
display:flex;
flex-wrap:wrap;
gap:10px 14px;
max-height:240px;
overflow:auto;
border:1px solid #ddd;
padding:10px;
background:#fff;
border-radius:6px;
align-content:flex-start;
}
.ws-tag-item{
display:inline-flex;
align-items:center;
gap:6px;
margin:0;
}
.ws-tag-item input{
margin:0;
}
</style>
';
echo '<div id="ws-product-tag-wrap">';
foreach ($terms as $t) {
$checked = in_array((int)$t->term_id, $selected_ids, true) ? 'checked' : '';
printf(
'<label class="ws-tag-item">
<input type="checkbox" name="ws_product_tag_ids[]" value="%d" %s>
<span>%s</span>
</label>',
(int) $t->term_id,
$checked,
esc_html($t->name)
);
}
echo '</div>';
}
add_action('save_post_product', function ($post_id) {
if (!isset($_POST['ws_product_tag_checklist_nonce']) ||
!wp_verify_nonce($_POST['ws_product_tag_checklist_nonce'], 'ws_save_product_tag_checklist')) {
return;
}
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
if (!current_user_can('edit_post', $post_id)) return;
$ids = isset($_POST['ws_product_tag_ids']) ? array_map('intval', (array) $_POST['ws_product_tag_ids']) : [];
wp_set_object_terms($post_id, $ids, 'product_tag', false);
});