Skip to content

Commit

Permalink
Send $product_name through woocommerce_cart_item_name filter once (#3…
Browse files Browse the repository at this point in the history
…8999)

* Send $product_name through woocommerce_cart_item_name filter only once. Closes #38744.

* Replace additional product name filters, with already filtered variable.

* add additional params to docblock of woocommerce_cart_item_name

* bump template version to 7.9.0

* revert cart_item_name filter change in #37830

* Minor fixes

* Bump template version

---------

Co-authored-by: Ron Rennick <ronald.rennick@automattic.com>
Co-authored-by: Jorge A. Torres <jorge.torres@automattic.com>
  • Loading branch information
3 people committed Jul 4, 2023
1 parent baca2fb commit 9d5c90d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 20 deletions.
4 changes: 4 additions & 0 deletions plugins/woocommerce/changelog/issues-38744-duplicate-filter
@@ -0,0 +1,4 @@
Significance: patch
Type: fix

Eliminate woocommerce_cart_item_name filter running twice on cart.php template
25 changes: 9 additions & 16 deletions plugins/woocommerce/templates/cart/cart.php
Expand Up @@ -12,7 +12,7 @@
*
* @see https://docs.woocommerce.com/document/template-structure/
* @package WooCommerce\Templates
* @version 7.8.0
* @version 7.9.0
*/

defined( 'ABSPATH' ) || exit;
Expand Down Expand Up @@ -43,8 +43,10 @@
/**
* Filter the product name.
*
* @since 7.8.0
* @since 2.1.0
* @param string $product_name Name of the product in the cart.
* @param array $cart_item The product in the cart.
* @param string $cart_item_key Key for the product in the cart.
*/
$product_name = apply_filters( 'woocommerce_cart_item_name', $_product->get_name(), $cart_item, $cart_item_key );

Expand All @@ -61,7 +63,7 @@
'<a href="%s" class="remove" aria-label="%s" data-product_id="%s" data-product_sku="%s">&times;</a>',
esc_url( wc_get_cart_remove_url( $cart_item_key ) ),
/* translators: %s is the product name */
esc_attr( sprintf( __( 'Remove %s from cart', 'woocommerce' ), $product_name ) ),
esc_attr( sprintf( __( 'Remove %s from cart', 'woocommerce' ), wp_strip_all_tags( $product_name ) ) ),
esc_attr( $product_id ),
esc_attr( $_product->get_sku() )
),
Expand All @@ -85,23 +87,14 @@
<td class="product-name" data-title="<?php esc_attr_e( 'Product', 'woocommerce' ); ?>">
<?php
if ( ! $product_permalink ) {
/**
* Filter the product name.
*
* @since 7.8.0
* @param string $product_name Name of the product in the cart.
* @param array $cart_item The product in the cart.
* @param string $cart_item_key Key for the product in the cart.
*/
echo wp_kses_post( apply_filters( 'woocommerce_cart_item_name', $product_name, $cart_item, $cart_item_key ) . '&nbsp;' );
echo wp_kses_post( $product_name . '&nbsp;' );
} else {
/**
* Filter the product name.
* This filter is documented above.
*
* @since 7.8.0
* @param string $product_url URL the product in the cart.
* @since 2.1.0
*/
echo wp_kses_post( apply_filters( 'woocommerce_cart_item_name', sprintf( '<a href="%s">%s</a>', esc_url( $product_permalink ), $product_name ), $cart_item, $cart_item_key ) );
echo wp_kses_post( apply_filters( 'woocommerce_cart_item_name', sprintf( '<a href="%s">%s</a>', esc_url( $product_permalink ), $_product->get_name() ), $cart_item, $cart_item_key ) );
}

do_action( 'woocommerce_after_cart_item_name', $cart_item, $cart_item_key );
Expand Down
8 changes: 4 additions & 4 deletions plugins/woocommerce/templates/cart/mini-cart.php
Expand Up @@ -14,7 +14,7 @@
*
* @see https://docs.woocommerce.com/document/template-structure/
* @package WooCommerce\Templates
* @version 7.8.0
* @version 7.9.0
*/

defined( 'ABSPATH' ) || exit;
Expand All @@ -33,9 +33,9 @@

if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters( 'woocommerce_widget_cart_item_visible', true, $cart_item, $cart_item_key ) ) {
/**
* Filter the product name.
* This filter is documented in woocommerce/templates/cart/cart.php.
*
* @param string $product_name Name of the product in the cart.
* @since 2.1.0
*/
$product_name = apply_filters( 'woocommerce_cart_item_name', $_product->get_name(), $cart_item, $cart_item_key );
$thumbnail = apply_filters( 'woocommerce_cart_item_thumbnail', $_product->get_image(), $cart_item, $cart_item_key );
Expand All @@ -50,7 +50,7 @@
'<a href="%s" class="remove remove_from_cart_button" aria-label="%s" data-product_id="%s" data-cart_item_key="%s" data-product_sku="%s">&times;</a>',
esc_url( wc_get_cart_remove_url( $cart_item_key ) ),
/* translators: %s is the product name */
esc_attr( sprintf( __( 'Remove %s from cart', 'woocommerce' ), $product_name ) ),
esc_attr( sprintf( __( 'Remove %s from cart', 'woocommerce' ), wp_strip_all_tags( $product_name ) ) ),
esc_attr( $product_id ),
esc_attr( $cart_item_key ),
esc_attr( $_product->get_sku() )
Expand Down

0 comments on commit 9d5c90d

Please sign in to comment.