Commit e14961c95ef for woocommerce
commit e14961c95ef01890c0ae39259ce5225be3dd12b5
Author: Alefe Souza <contact@alefesouza.com>
Date: Tue Oct 13 13:44:29 2026 -0300
Pass the cart item to the classic cart's backorder notification filter (#69669)
diff --git a/plugins/woocommerce/changelog/35915-backorder-notification-cart-item b/plugins/woocommerce/changelog/35915-backorder-notification-cart-item
new file mode 100644
index 00000000000..f75641b3b34
--- /dev/null
+++ b/plugins/woocommerce/changelog/35915-backorder-notification-cart-item
@@ -0,0 +1,4 @@
+Significance: minor
+Type: dev
+
+Pass the cart item and its key to the `woocommerce_cart_item_backorder_notification` filter, so the notice can vary per variation.
diff --git a/plugins/woocommerce/templates/cart/cart.php b/plugins/woocommerce/templates/cart/cart.php
index 45bcd8f3cc7..c80c1fd4341 100644
--- a/plugins/woocommerce/templates/cart/cart.php
+++ b/plugins/woocommerce/templates/cart/cart.php
@@ -12,7 +12,7 @@
*
* @see https://woocommerce.com/document/template-structure/
* @package WooCommerce\Templates
- * @version 11.2.0
+ * @version 11.3.0
*/
defined( 'ABSPATH' ) || exit;
@@ -130,7 +130,21 @@ do_action( 'woocommerce_before_cart' ); ?>
// Backorder notification.
if ( $_product->backorders_require_notification() && $_product->is_on_backorder( $cart_item['quantity'] ) ) {
- echo wp_kses_post( apply_filters( 'woocommerce_cart_item_backorder_notification', '<p class="backorder_notification">' . esc_html__( 'Available on backorder', 'woocommerce' ) . '</p>', $product_id ) );
+ /**
+ * Filter the backorder notification shown for a cart item.
+ *
+ * Themes with an outdated copy of this template pass only the first two arguments, so callbacks that
+ * accept `$cart_item` and `$cart_item_key` should give them default values, e.g. `$cart_item = null`.
+ *
+ * @param string $notification The backorder notification HTML.
+ * @param int $product_id The product ID. For a variation, this is the parent product ID.
+ * @param array $cart_item The cart item data, including `variation_id` and the product in `data`.
+ * @param string $cart_item_key Unique key for the cart item.
+ *
+ * @since 3.4.0
+ * @since 11.3.0 Added the `$cart_item` and `$cart_item_key` parameters.
+ */
+ echo wp_kses_post( apply_filters( 'woocommerce_cart_item_backorder_notification', '<p class="backorder_notification">' . esc_html__( 'Available on backorder', 'woocommerce' ) . '</p>', $product_id, $cart_item, $cart_item_key ) );
}
?>
</td>
diff --git a/plugins/woocommerce/tests/php/includes/class-wc-cart-test.php b/plugins/woocommerce/tests/php/includes/class-wc-cart-test.php
index cf62b8a466d..b2e16a262ed 100644
--- a/plugins/woocommerce/tests/php/includes/class-wc-cart-test.php
+++ b/plugins/woocommerce/tests/php/includes/class-wc-cart-test.php
@@ -672,6 +672,40 @@ class WC_Cart_Test extends \WC_Unit_Test_Case {
}
}
+ /**
+ * @testdox The cart template passes the cart item and its key to the backorder notification filter, so callbacks can tell variations apart.
+ */
+ public function test_cart_template_passes_cart_item_to_backorder_notification_filter(): void {
+ list( $product, $variation ) = WC_Helper_Product::create_variation_product_with_global_attributes(
+ 'Backorder Variation Product',
+ array(
+ 'pa_size' => 'huge',
+ 'pa_number' => '',
+ )
+ );
+ $variation->set_manage_stock( true );
+ $variation->set_stock_quantity( 0 );
+ $variation->set_backorders( 'notify' );
+ $variation->save();
+
+ $received = array();
+ $capture = function ( $notification, $product_id, $cart_item = null, $cart_item_key = null ) use ( &$received ) {
+ $received = array( $product_id, $cart_item, $cart_item_key );
+ return '<p class="backorder_notification">Variation ' . ( $cart_item['variation_id'] ?? 'unknown' ) . ' ships later</p>';
+ };
+ add_filter( 'woocommerce_cart_item_backorder_notification', $capture, 10, 4 );
+
+ list( $cart_item_key ) = $this->add_variation_to_cart( $product, $variation );
+
+ $html = wc_get_template_html( 'cart/cart.php' );
+
+ $this->assertSame( $product->get_id(), $received[0], 'The product ID argument stays the parent ID for backward compatibility.' );
+ $this->assertIsArray( $received[1], 'The cart item should be passed to the filter.' );
+ $this->assertSame( $variation->get_id(), $received[1]['variation_id'] );
+ $this->assertSame( $cart_item_key, $received[2] );
+ $this->assertStringContainsString( 'Variation ' . $variation->get_id() . ' ships later', $html );
+ }
+
/**
* @testdox Cart item metadata dedup keys on the template-provided name regardless of name filters.
*/