Filter uncanny-learndash-groups

ulgm_wc_min_qty_error_message

Filters the minimum quantity error message for WooCommerce products when a minimum quantity is not met.

add_filter( 'ulgm_wc_min_qty_error_message', $callback, 10, 3 );

Description

Filters the error message displayed when the minimum quantity for a product in WooCommerce is not met. Developers can modify this message to provide custom feedback to users regarding minimum purchase requirements. The parameters include the original message, the product's minimum quantity, and the current quantity in the cart.


Usage

add_filter( 'ulgm_wc_min_qty_error_message', 'your_function_name', 10, 3 );

Parameters

$s (mixed)
This parameter represents the error message string that will be displayed to the user.
$product_min (mixed)
This parameter represents the error message that will be displayed.
$already_in_cart (mixed)
This parameter represents the minimum quantity allowed for the product.

Return Value

The filtered value.


Examples

/**
 * Filter the error message for minimum quantity validation in WooCommerce.
 *
 * This function customizes the error message displayed to the user when they attempt to add
 * a product to the cart that doesn't meet the minimum quantity requirement.
 *
 * @param string $s The default error message string.
 * @param int    $product_min The minimum quantity required for the product.
 * @param int    $already_in_cart The current quantity of the product already in the cart.
 * @return string The modified error message string.
 */
add_filter( 'ulgm_wc_min_qty_error_message', function( $s, $product_min, $already_in_cart ) {

	// Get the product object to retrieve its title.
	// Note: The original source context doesn't provide $product_id directly to this filter,
	// so we'll need to assume it's accessible within the scope where this filter is applied,
	// or the calling function provides it. For this example, we'll assume $product_id
	// is available in the scope, which is typical for how WordPress hooks work.
	// If $product_id is not available, a more complex approach might be needed.
	// Let's assume for this realistic example that $product_id is available.
	global $product_id; // This is a common, though not always ideal, way to access context in WP hooks.
	$product = wc_get_product( $product_id );

	if ( ! $product ) {
		return $s; // Return default if product not found.
	}

	$product_title = $product->get_title();
	$cart_url      = esc_url( wc_get_cart_url() );
	$cart_text     = __( 'your cart', 'uncanny-learndash-groups' );

	// Construct a more informative and user-friendly error message.
	$new_message = sprintf(
		__( 'Oops! You need to add at least %1$s of %2$s to %3$s. Currently, you have %4$d in your cart. Please add %5$s more.', 'your-text-domain' ),
		'<strong>' . $product_min . '</strong>',
		'<em>' . $product_title . '</em>',
		'<a href="' . $cart_url . '">' . $cart_text . '</a>',
		$already_in_cart,
		'<strong>' . absint( $product_min - $already_in_cart ) . '</strong>'
	);

	// Apply any further filters to the new message.
	return apply_filters( 'ulgm_wc_min_qty_error_message_custom', $new_message, $product_min, $already_in_cart, $product_title, $cart_url, $cart_text );

}, 10, 3 );

Placement

This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.


Source Code

src/classes/woocommerce/woocommerce-min-max-quantity.php:276

public function woocommerce_qty_add_to_cart_validation( $passed, $product_id, $quantity, $variation_id = '', $variations = '' ) {

		$product_min = $this->woocommerce_get_product_min_limit( $product_id );
		$product_max = $this->woocommerce_get_product_max_limit( $product_id );

		if ( ! is_numeric( $product_min ) && ! is_numeric( $product_max ) ) {
			return $passed;
		}

		$already_in_cart = $this->woocommerce_qty_get_cart_qty( $product_id );
		$product         = wc_get_product( $product_id );
		if ( ! ( $product instanceof WC_Product ) ) {
			return $passed;
		}
		$product_title = $product->get_title();

		if ( is_numeric( $product_max ) && is_numeric( $already_in_cart ) ) {

			if ( ( $already_in_cart + $quantity ) > $product_max ) {
				// oops. too much.
				$passed = false;
				$diff   = absint( $product_max - $already_in_cart );
				wc_add_notice(
					apply_filters(
						'ulgm_wc_max_qty_error_message',
						sprintf(
							__( "You can add a maximum of %1$s %2$s's to %3$s. You had %4$d in the cart, %4$d more added.", 'uncanny-learndash-groups' ),
							$product_max,
							$product_title,
							'<a href="' . esc_url( wc_get_cart_url() ) . '">' . __( 'your cart', 'uncanny-learndash-groups' ) . '</a>',
							$already_in_cart
						),
						$product_max,
						$already_in_cart,
						$diff
					),
					'error'
				);
				WC()->cart->add_to_cart( $product_id, $diff );
			}
			if ( ( $already_in_cart + $quantity ) < $product_min ) {
				// oops. not enough.
				$passed = false;
				wc_add_notice(
					apply_filters(
						'ulgm_wc_min_qty_error_message',
						sprintf(
							__( "You need to add a minimum of %1$s %2$s's to %3$s.", 'uncanny-learndash-groups' ),
							$product_min,
							$product_title,
							'<a href="' . esc_url( wc_get_cart_url() ) . '">' . __( 'your cart', 'uncanny-learndash-groups' ) . '</a>'
						),
						$product_min,
						$already_in_cart
					),
					'error'
				);
				WC()->cart->add_to_cart( $product_id, $product_min );
			}
		}

		return $passed;
	}


Scroll to Top