The following will work with all product types (simple, variable, variations…), disabling item links from an array of defined product Ids:
// Cart item link
add_filter( 'woocommerce_cart_item_name', 'conditionally_remove_link_from_cart_item_name', 10, 3 );
function conditionally_remove_link_from_cart_item_name( $item_name, $cart_item, $cart_item_key ) {
// HERE set your products IDs in the array
$product_ids = array(37, 40);
if( array_intersect($product_ids, array($cart_item['product_id'], $cart_item['variation_id']) ) ) {
$item_name = $cart_item['data']->get_name();
}
return $item_name;
}
// Mini-cart item link
add_filter( 'woocommerce_cart_item_permalink', 'conditionally_remove_cart_item_permalink', 10, 3 );
function conditionally_remove_cart_item_permalink( $permalink, $cart_item, $cart_item_key ) {
// HERE set your products IDs in the array
$product_ids = array(37, 40);
if( array_intersect($product_ids, array($cart_item['product_id'], $cart_item['variation_id']) ) ) {
$permalink = '';
}
return $permalink;
}
// Order item link
add_filter( 'woocommerce_order_item_name', 'conditionally_remove_link_from_order_item_name', 10, 2 );
function conditionally_remove_link_from_order_item_name( $item_name, $item ) {
// HERE set your products IDs in the array
$product_ids = array(37, 40);
if( array_intersect($product_ids, array($item->get_product_id(), $item->get_variation_id()) ) ) {
$item_name = $item->get_name();
}
return $item_name;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.