Filter uncanny-learndash-groups

ulgm_group_name_required

Filters whether the group name is required, allowing modification before the check is performed.

add_filter( 'ulgm_group_name_required', $callback, 10, 1 );

Description

Filters whether a group name is required for a license. By default, it is required. Return `false` to disable the requirement, allowing licenses to be added without a group name.


Usage

add_filter( 'ulgm_group_name_required', 'your_function_name', 10, 1 );

Return Value

The filtered value.


Examples

add_filter( 'ulgm_group_name_required', 'my_custom_group_name_requirement', 10, 1 );

/**
 * Conditionally make the group name field required based on a custom product category.
 *
 * This example makes the group name field required only if a product from the
 * 'premium-licenses' custom product category is in the cart.
 *
 * @param bool $is_required The default requirement status (true).
 * @return bool The new requirement status.
 */
function my_custom_group_name_requirement( $is_required ) {
	// If the group name is already marked as not required, respect that.
	if ( ! $is_required ) {
		return false;
	}

	$premium_category_in_cart = false;
	$premium_category_slug    = 'premium-licenses'; // Replace with your actual custom category slug

	// Loop through cart items to check for the specific product category.
	foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
		$product_id = $cart_item['product_id'];
		$product    = wc_get_product( $product_id );

		// Check if the product belongs to the premium license category.
		if ( has_term( $premium_category_slug, 'product_cat', $product->get_id() ) ) {
			$premium_category_in_cart = true;
			break; // No need to check further if found.
		}
	}

	// If the premium category product is in the cart, the group name is required.
	// Otherwise, it's not required.
	return $premium_category_in_cart;
}

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-license.php:1393
src/classes/woocommerce/woocommerce-license.php:2040

public function process_group_name() {

		if ( WoocommerceModifyGroup::is_modifying_license() ) {
			return;
		}

		if ( false === apply_filters( 'ulgm_group_name_required', true ) ) {
			return;
		}

		foreach ( WC()->cart->get_cart() as $key => $val ) {
			$product_id = $val['product_id'];
			$product    = wc_get_product( $product_id );

			if ( false === self::is_license_or_subscription_license( $product ) ) {
				continue;
			}

			$per_seat_text = get_option( 'ulgm_per_seat_text', 'Seat' );

			if ( ! ulgm_filter_has_var( 'ulgm_group_name_' . $key, INPUT_POST ) || empty( ulgm_filter_input( 'ulgm_group_name_' . $key, INPUT_POST ) ) ) {
				wc_add_notice( sprintf( '%s %s x %d %s(s)', esc_html__( 'Please enter group name for:', 'uncanny-learndash-groups' ), $product->get_name(), $val['quantity'], $per_seat_text ), 'error' );
			}
		}
	}


Scroll to Top