Commit 1198ec1d272 for woocommerce
commit 1198ec1d2725ad27e18c46af4981b4848e9c029b
Author: Mike Jolley <mike.jolley@me.com>
Date: Mon Jun 15 17:26:05 2026 +0100
Guard mini-cart and review-order templates against non-products (#65685)
* Guard mini-cart and review-order templates against non-product cart items
A filter hooked on woocommerce_cart_item_product that returns a truthy
non-product value (true, array, string, stdClass) bypassed the existing
$_product && $_product->exists() check and fatalled on ->exists(). Switch
both templates to an instanceof WC_Product guard, matching the pattern
already merged into cart.php in #64252.
Also extract the per-item visibility filter into a documented $visible
variable above the guard, mirroring cart.php, so the inline hook has the
docblock required by WooCommerce.Commenting.CommentHooks. Bumps template
@version headers.
Follow-up to #64252.
* Update plugins/woocommerce/templates/cart/mini-cart.php
Co-authored-by: Raluca Stan <ralucastn@gmail.com>
---------
Co-authored-by: Raluca Stan <ralucastn@gmail.com>
diff --git a/plugins/woocommerce/changelog/pcb-201-harden-cart-item-product-guards b/plugins/woocommerce/changelog/pcb-201-harden-cart-item-product-guards
new file mode 100644
index 00000000000..fc8b28bdcc1
--- /dev/null
+++ b/plugins/woocommerce/changelog/pcb-201-harden-cart-item-product-guards
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Prevent a fatal error in the mini-cart and checkout review-order templates when a filter hooked on woocommerce_cart_item_product returns a truthy non-product value.
diff --git a/plugins/woocommerce/templates/cart/mini-cart.php b/plugins/woocommerce/templates/cart/mini-cart.php
index 638f4b27ae1..af1c260d2da 100644
--- a/plugins/woocommerce/templates/cart/mini-cart.php
+++ b/plugins/woocommerce/templates/cart/mini-cart.php
@@ -14,7 +14,7 @@
*
* @see https://woocommerce.com/document/template-structure/
* @package WooCommerce\Templates
- * @version 10.0.0
+ * @version 11.0.0
*/
defined( 'ABSPATH' ) || exit;
@@ -31,7 +31,17 @@ do_action( 'woocommerce_before_mini_cart' ); ?>
$_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
$product_id = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );
- if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters( 'woocommerce_widget_cart_item_visible', true, $cart_item, $cart_item_key ) ) {
+ /**
+ * Filter whether this cart item is visible in the mini-cart.
+ *
+ * @since 1.6.0
+ * @param bool $visible Whether the cart item is visible. Default true.
+ * @param array $cart_item The cart item data.
+ * @param string $cart_item_key The cart item key.
+ */
+ $visible = apply_filters( 'woocommerce_widget_cart_item_visible', true, $cart_item, $cart_item_key );
+
+ if ( $_product instanceof WC_Product && $_product->exists() && $cart_item['quantity'] > 0 && $visible ) {
/**
* This filter is documented in woocommerce/templates/cart/cart.php.
*
diff --git a/plugins/woocommerce/templates/checkout/review-order.php b/plugins/woocommerce/templates/checkout/review-order.php
index 11bba1595dd..08a3459d057 100644
--- a/plugins/woocommerce/templates/checkout/review-order.php
+++ b/plugins/woocommerce/templates/checkout/review-order.php
@@ -12,7 +12,7 @@
*
* @see https://woocommerce.com/document/template-structure/
* @package WooCommerce\Templates
- * @version 5.2.0
+ * @version 11.0.0
*/
defined( 'ABSPATH' ) || exit;
@@ -31,7 +31,17 @@ defined( 'ABSPATH' ) || exit;
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
- if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters( 'woocommerce_checkout_cart_item_visible', true, $cart_item, $cart_item_key ) ) {
+ /**
+ * Filter whether this cart item is visible in the checkout review order table.
+ *
+ * @since 2.1.0
+ * @param bool $visible Whether the cart item is visible. Default true.
+ * @param array $cart_item The cart item data.
+ * @param string $cart_item_key The cart item key.
+ */
+ $visible = apply_filters( 'woocommerce_checkout_cart_item_visible', true, $cart_item, $cart_item_key );
+
+ if ( $_product instanceof WC_Product && $_product->exists() && $cart_item['quantity'] > 0 && $visible ) {
?>
<tr class="<?php echo esc_attr( apply_filters( 'woocommerce_cart_item_class', 'cart_item', $cart_item, $cart_item_key ) ); ?>">
<td class="product-name">