ulgm_wc_max_qty_error_message
Filters the maximum quantity error message displayed for WooCommerce products.
add_filter( 'ulgm_wc_max_qty_error_message', $callback, 10, 4 );
Description
This filter hook, `ulgm_wc_max_qty_error_message`, allows developers to customize the error message displayed when a WooCommerce product exceeds its maximum quantity limit. It fires during the add-to-cart validation process. Developers can modify the default message by returning a new string. The hook provides access to the product's maximum quantity and the current quantity in the cart.
Usage
add_filter( 'ulgm_wc_max_qty_error_message', 'your_function_name', 10, 4 );
Parameters
-
$s(mixed) - This parameter represents the validation status, typically a boolean, indicating whether the quantity validation has passed or failed.
-
$product_max(mixed) - This parameter is a boolean indicating whether the validation passed.
-
$already_in_cart(mixed) - This parameter holds the maximum quantity allowed for the product.
-
$diff(mixed) - This parameter contains the current quantity of the product that is already in the user's cart.
Return Value
The filtered value.
Examples
<?php
/**
* Filters the maximum quantity error message for WooCommerce products.
*
* This function allows developers to customize the error message displayed
* when a user attempts to add more of a product to their cart than the
* maximum allowed quantity.
*
* @param string $s The default error message string.
* @param int $product_max The maximum quantity allowed for the product.
* @param int $already_in_cart The current quantity of the product already in the cart.
* @param int $diff The difference between the max quantity and what's already in the cart.
* @return string The customized error message.
*/
add_filter( 'ulgm_wc_max_qty_error_message', function( $s, $product_max, $already_in_cart, $diff ) {
// Get the product object to retrieve its title.
// We assume $product_id is available in the scope where this filter is applied
// or can be retrieved. For this example, let's assume it's passed indirectly
// or can be obtained from the global WooCommerce cart data.
// In a real scenario, $product_id would likely be accessible from the context
// where the original add_to_cart_validation runs.
// For demonstration, let's simulate getting a product.
// In the actual context of ulgm_wc_max_qty_error_message, $product_id
// might not be directly passed. We might need to infer it or have it
// passed by the source function if it were designed differently.
// However, since we are responding to the hook signature, we'll focus on
// manipulating the message with the provided parameters.
// Let's assume we know the product title, for example, if it was passed
// as part of another parameter or is globally available in this context.
// For a realistic example, let's pretend we got the product title.
// If the product title isn't directly available, we'd have to make assumptions
// or adjust the original function to pass it.
$product_title = __( 'items', 'your-text-domain' ); // Placeholder if product title isn't available.
// You can also try to retrieve the product if the ID is somehow available in this scope.
// This is a bit speculative without the full context of how the filter is called.
// If $product_id was available:
// $product = wc_get_product( $product_id );
// if ( $product ) {
// $product_title = $product->get_title();
// }
// Example of a more user-friendly message, mentioning the quantity *remaining*
// that can be added instead of how many more are being added.
$remaining_to_add = $product_max - $already_in_cart;
$custom_message = sprintf(
__( 'You have reached the maximum quantity for this product. You can add %1$s more %2$s to your cart. Your current limit is %3$s %2$s.', 'your-text-domain' ),
$remaining_to_add,
$product_title,
$product_max
);
// You could also add more complex logic, like suggesting to remove items from the cart.
if ( $already_in_cart > 0 ) {
$custom_message .= sprintf(
__( ' You currently have %1$s %2$s in your cart.', 'your-text-domain' ),
$already_in_cart,
$product_title
);
}
// Always return the filtered value.
return $custom_message;
}, 10, 4 ); // Priority 10, 4 arguments accepted.
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:255
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;
}