diff --git a/includes/class-wc-cart.php b/includes/class-wc-cart.php
index 1103ee053e5..966a5bb7fdf 100644
--- a/includes/class-wc-cart.php
+++ b/includes/class-wc-cart.php
@@ -1218,15 +1218,30 @@ class WC_Cart extends WC_Legacy_Cart {
$products_qty_in_cart = $this->get_cart_item_quantities();
if ( isset( $products_qty_in_cart[ $product_data->get_stock_managed_by_id() ] ) && ! $product_data->has_enough_stock( $products_qty_in_cart[ $product_data->get_stock_managed_by_id() ] + $quantity ) ) {
- throw new Exception(
- sprintf(
- '%s %s',
- wc_get_cart_url(),
- __( 'View cart', 'woocommerce' ),
- /* translators: 1: quantity in stock 2: current quantity */
- sprintf( __( 'You cannot add that amount to the cart — we have %1$s in stock and you already have %2$s in your cart.', 'woocommerce' ), wc_format_stock_quantity_for_display( $product_data->get_stock_quantity(), $product_data ), wc_format_stock_quantity_for_display( $products_qty_in_cart[ $product_data->get_stock_managed_by_id() ], $product_data ) )
- )
+ $stock_quantity = $product_data->get_stock_quantity();
+ $stock_quantity_in_cart = $products_qty_in_cart[ $product_data->get_stock_managed_by_id() ];
+
+ $message = printf(
+ '%s %s',
+ wc_get_cart_url(),
+ __( 'View cart', 'woocommerce' ),
+ /* translators: 1: quantity in stock 2: current quantity */
+ sprintf( __( 'You cannot add that amount to the cart — we have %1$s in stock and you already have %2$s in your cart.', 'woocommerce' ), wc_format_stock_quantity_for_display( $stock_quantity, $product_data ), wc_format_stock_quantity_for_display( $stock_quantity_in_cart, $product_data ) )
);
+
+ /**
+ * Filters message about product not having enough stock accounting for what's already in the cart.
+ *
+ * @param string $message Message.
+ * @param WC_Product $product_data Product data.
+ * @param int $stock_quantity Quantity remaining.
+ * @param int $stock_quantity_in_cart
+ *
+ * @since 5.0.0
+ */
+ $message = apply_filters( 'woocommerce_cart_product_not_enough_stock_already_in_cart_message', $message, $product_data, $stock_quantity, $stock_quantity_in_cart );
+
+ throw new Exception( $message );
}
}