Product subcat with duplicate name of a subcat of another cat gets the category name in the slug

It took me two days to figure this out in the proper way, so I’ll add it here for anyone else that may need it in the future.

add_filter('wp_unique_term_slug', 'prevent_cat_suffix', 10, 3);
function prevent_cat_suffix($slug, $term, $original_slug)
{
    if ($term->post_type == 'product' && $term->parent !== 0) {
        return $original_slug;
    }

    return $slug;
}

As you can see in the code, I’ve narrowed it down to products only, but you can make it suitable for any other post type also by removing the $term->post_type == 'product' && in the code above.

error code: 523