4

I would like to override the woocommerce_cart_item_name hyperlink in order to direct the user to my own page when they click on the name of the product in the cart. I do not want to modify the woocommerce cart.php or place a modified cart.php in my child theme WooCommerce directory. I have tried adding the following to my functions.php, but it does not work. My guess is that $_product->get_title() does not work within the scope of functions.php. What is the correct method that uses add_filter to override the woocommerce_cart_item_name? Thank you.

/* Function that returns custom product hyperlink */
function wc_cart_item_name_hyperlink() {
    return sprintf( '<a href="%s">%s </a>','example.com/mypage/', $_product->get_title() );
}
/* Filter to override cart_item_name */
add_filter( 'woocommerce_cart_item_name', 'wc_cart_item_name_hyperlink' );
0

1 Answer 1

6

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="%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.

6
  • 1
    Your suggestion worked perfectly! Thank you! I'm brand new to WooCommerce and a WordPress novice, so forgive me for asking what is probably a remedial question: why did you need to pass the argument ($link_text) and why did you include the arguments (10, 2) in add_filter()? Thank you very much for your help.
    – Tony
    Commented Feb 2, 2016 at 0:09
  • Ok - I answered my own question about the add_filter arguments: add_filter( $tag, $function_to_add, $priority, $accepted_args ); Still not sure why it is necessary to pass $link_text. Thank you again for your help.
    – Tony
    Commented Feb 2, 2016 at 0:26
  • I believe I just answered my other question: I'm guessing that $link_text is the default argument and that any other arguments are passed in addition to $link_text (thus $accepted_args = 2). Do you know where I would look to see the list of methods (like get_title() ) available to $product_data['product_id'] and $product_data['data']? Thank you very much for your help.
    – Tony
    Commented Feb 2, 2016 at 14:35
  • Take a look at the WooCommerce documentation, particularly this page: docs.woothemes.com/wc-apidocs/class-WC_Product_Simple.html Commented Feb 3, 2016 at 14:41
  • And you figured out the answer to your other question correctly I think. You don't need the $link_text but it is one of the function arguments and because of how PHP works, if you left out $link_text, then you wouldn't be able to use the $product_data argument either. Here's the PHP page on function arguments: php.net/manual/en/functions.arguments.php Commented Feb 3, 2016 at 14:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.