add_filter to modify woocommerce_cart_item_name hyperlink

The key is to pass arguments to the callback function.

Try this:

/* Function that returns custom product hyperlink */
function wc_cart_item_name_hyperlink( $link_text, $product_data ) {
   $title = get_the_title($product_data['product_id']);
   return sprintf( '<a href="https://wordpress.stackexchange.com/questions/216353/%s">%s </a>','example.com/mypage/',$title );
}
/* Filter to override cart_item_name */
add_filter( 'woocommerce_cart_item_name', 'wc_cart_item_name_hyperlink', 10, 2 );

The $product_data parameter is an array that contains the product_id, variation_id, quantity, line_total, line_tax, line_subtotal, line_subtotal_tax, line_tax_data, as well as an inner array called variation that has more details.

Also, $product_data['data'] is the WC_Product_Simple object, which give you even more to work with.

Leave a Comment