Try that:
foreach ( $ancestors as $ancestor ) {
$ancestor = get_term( $ancestor, ‘product_cat’ );
if ( ! is_wp_error( $ancestor ) && $ancestor )
$the_ancestor_slug = ‘/’ . $ancestor->slug;
}
return home_url( $wcbp_base . $the_ancestor_slug . ‘/’ . $main_term->slug . ‘/’ . $post->post_name );