How to make a custom redirect in WooCommerce?

add_action('template_redirect', 'woo_custom_redirect');

function woo_custom_redirect($redirect) {
    // HERE set your product category (can be term IDs, slugs or names)
    $category = 'subscription';

    $found = false;

    // CHECK CART ITEMS: search for items from our product category
    foreach(WC() - > cart - > get_cart() as $cart_item) {
            if (has_term($category, 'product_cat', $cart_item['product_id'])) {
                    $found = true;
                    break;
            }
    }

    if (!is_user_logged_in() && is_checkout() && $found) {
            wp_redirect(get_permalink(get_option('woocommerce_myaccount_page_id')));
            exit();
    }
}