Disable link to product in WooCommerce Order page (in My Account area).
In the WooCommerce Community Facebook group a member asked whether it was possible to disable the product link in the Order page in the My Account area. It is and it was one line of code!
The member explained that the product was a rental and cannot be repurchased. Whatever the reason, achieving it is easy.
How did I figure out the code to use?
When you are dealing with a large code base like WordPress you are unlikely be able to browse some random files and find the code you need so you need a different strategy. I always look for a piece of text from the front end to get me close.
When I looked at the source for the Order page I saw that the product link was in a table cell with ‘woocommerce-table__product-name product-name‘ classes. I found this in woocommerce/templates/order/order-details-item.php. As it is in a template file I could copy it to the theme folder and change as necessary. The disadvantage is that when there is an update to WooCommerce the template file may change and you will have to merge the updates into your copy of the file.
Luckily removing the product link does not require editing the template file – there is a filter to help us. Here’s the relevant code from the template.
$is_visible = $product && $product->is_visible(); $product_permalink = apply_filters( 'woocommerce_order_item_permalink', $is_visible ? $product->get_permalink( $item ) : '', $item, $order ); echo wp_kses_post( apply_filters( 'woocommerce_order_item_name', $product_permalink ? sprintf( '<a href="%s">%s</a>', $product_permalink, $item->get_name() ) : $item->get_name(), $item, $is_visible ) );
The third line displays the product link (or just the product name), depending on whether
$product_permalink is true or false. The line above it sets that variable and the ‘woocommerce_order_item_permalink‘ filter can change it. If our filter function returns false then we should only get the product name. Instead of writing a function to ‘return false’ we can use the __return_false function as it does just that. Nice and short.