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.
Thank you for sharing! However, is there any other way to disable product link in WooCommerce shop_order page （backend) for specific user role? Because I found that they could access to edit the product via the order item link.
Nikkie and I exchanged a few emails. I suggested using the User Role Editor plugin to change the capabilities of the Shop Manager. She deactivated all the Posts capabilities and got the result she needed – the Shop Manager cannot edit products (the link is still there but useless).
Darryl Gebien says
What about removing the product hyperlink from the Order Received (Thank You) email?
@Darryl – Only downloadable products have a link in the ‘Thank You’ email. This link is created in woocommerce/templates/emails/email-downloads.php.
To remove this link you will have to copy that file into your theme folder (or child theme folder) and change the markup for the ‘download-product’ to remove the link e.g. change to:
<?php echo wp_kses_post( $download['product_name'] ); ?>