Skip to content

woocommerce_cart_item_name filter is applied twice #38744

@dennisnissle

Description

@dennisnissle

Prerequisites

  • I have carried out troubleshooting steps and I believe I have found a bug.
  • I have searched for similar bugs in both open and closed issues and cannot find a duplicate.

Describe the bug

The woocommerce_cart_item_name filter in the 7.8.0 cart.php template is applied twice. That leads to issues with plugins that use the filter to adjust the cart item name (as the additional info may be added twice).

Expected behavior

Do only apply the filter once or pass the original, non-filtered $_product->get_name() as first parameter to the next filter call instead of $product_name (which already has been filtered).

Actual behavior

The filter is being applied twice. First on creating the variable:

$product_name = apply_filters( 'woocommerce_cart_item_name', $_product->get_name(), $cart_item, $cart_item_key );

and during output:

echo wp_kses_post( apply_filters( 'woocommerce_cart_item_name', $product_name, $cart_item, $cart_item_key ) . ' ' );

Steps to reproduce

  1. Add a snippet to your functions.php:
add_filter( 'woocommerce_cart_item_name', function( $cart_item_name ) {
   return $cart_item_name . ' - test';
} );
  1. Add a product to cart
  2. See how the -test suffix is added twice.

WordPress Environment

Latest WP and Woo version. Latest storefront theme.

Isolating the problem

  • I have deactivated other plugins and confirmed this bug occurs when only WooCommerce plugin is active.
  • This bug happens with a default WordPress theme active, or Storefront.
  • I can reproduce this bug consistently using the steps above.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions