Here is a custom function that will show your custom message on cart page, with the category archive pages link for the first product added to cart.
If a product without a category is added to cart, nothing is displayed.
Optionally you can also display it on checkout page (uncommenting last line).
Here is this code:
function cart_custom_message() {
if( !WC()->cart->is_empty ){
foreach ( WC()->cart->get_cart() as $item ) {
$categories_obj = get_the_terms( $item['product_id'], 'product_cat' );
if($categories_obj) break;
}
if($categories_obj) {
foreach ( $categories_obj as $category ) {
break;
}
$category_id = $category->term_id;
$category_name = $category->name;
$category_slug = $category->slug;
$category_url = get_term_link( $category_slug, 'product_cat' );
$message = __("Add more T-shirts from <a href='$category_url'><strong>$category_name</strong></a> category and get a discount!", "your_theme_domain");
echo "<div class='woocommerce-info'>$message</div>";
}
}
}
add_action('woocommerce_before_cart', 'cart_custom_message');
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and fully functional.
References: