问题
I would like to disable the link from cart items and order items names for specific variable products.
I found Disable item name link for specific product in Woocommerce cart checkout and orders answer code that does disable links on single product, but I would like to know how to change it for a variables one?
回答1:
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.
来源:https://stackoverflow.com/questions/63395992/disable-item-link-for-specific-products-in-woocommerce-cart-checkout-and-orders