Filter uncanny-learndash-groups

ld_woocommerce_add_subscription_group_access

Filters whether a WooCommerce order grants access to a LearnDash subscription group.

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

Description

Fires when LearnDash is adding group access for a WooCommerce order. Allows developers to conditionally prevent access from being granted by returning `false`. The `$order` object is provided for context. This filter runs after the initial access check.


Usage

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

Parameters

$order (mixed)
This parameter is a boolean that allows you to conditionally prevent the addition of LearnDash group access when the filter is applied.

Return Value

The filtered value.


Examples

/**
 * Example function to modify group access based on WooCommerce order details.
 * This function adds or modifies access to LearnDash groups for a user
 * based on the products included in a WooCommerce order.
 *
 * @param mixed $return_value The default return value from the filter.
 * @param WC_Order $order The WooCommerce order object.
 * @param string $current_filter The name of the current filter being applied.
 * @return mixed Modified return value.
 */
add_filter( 'ld_woocommerce_add_subscription_group_access', function( $return_value, $order, $current_filter ) {

	// If the initial return value is false, it means the filter has already decided to stop processing.
	if ( false === $return_value ) {
		return false;
	}

	// Ensure we have a valid WooCommerce order object.
	if ( ! $order instanceof WC_Order ) {
		return $return_value;
	}

	$customer_id = $order->get_customer_id();

	// If there's no customer associated with the order, we can't grant access.
	if ( empty( $customer_id ) ) {
		return $return_value;
	}

	// Iterate through each item in the order.
	foreach ( $order->get_items() as $item_id => $item ) {
		$product_id = $item->get_product_id();
		$variation_id = $item->get_variation_id();

		$product_meta_id = ! empty( $variation_id ) ? $variation_id : $product_id;

		// Get the associated LearnDash group ID from product meta.
		// We're assuming '_related_group' meta key stores a single group ID or an array of group IDs.
		$group_ids = get_post_meta( $product_meta_id, '_related_group', true );

		if ( ! empty( $group_ids ) ) {
			// Ensure $group_ids is always an array for consistent processing.
			if ( ! is_array( $group_ids ) ) {
				$group_ids = array( $group_ids );
			}

			foreach ( $group_ids as $group_id ) {
				// Ensure we have a valid group ID.
				if ( absint( $group_id ) > 0 ) {
					// In a real scenario, you might call a LearnDash function here
					// to grant access to the group for the customer.
					// For example:
					// learndash_set_group_access( $group_id, $customer_id, $order->get_id() );

					// To simulate success, we'll just log or perform a dummy action.
					// In a real application, replace this with actual LearnDash access granting.
					error_log( sprintf( 'Granting access to Group ID %d for Customer ID %d from Order ID %d', $group_id, $customer_id, $order->get_id() ) );

					// In a real LearnDash integration, you would also handle revoking access
					// if the subscription is cancelled or the order is refunded,
					// likely in other WooCommerce hooks like 'woocommerce_order_status_changed'.

					// Clear relevant LearnDash transients to ensure updated data is fetched.
					delete_transient( 'learndash_user_groups_' . $customer_id );
					delete_transient( 'learndash_user_courses_' . $customer_id );
					delete_transient( 'learndash_group_users_' . $group_id );
				}
			}
		}
	}

	// Returning true indicates that the filter has successfully processed and no further action is needed
	// by the default implementation if the filter itself returned false.
	return true;

}, 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-learndash-groups.php:603

public static function add_subscription_group_access( $order ) {
		if ( ! apply_filters( 'ld_woocommerce_add_subscription_group_access', true, $order, current_filter() ) ) {
			return;
		}

		$products = $order->get_items();
		// $start_date = $order->get_date( 'start_date' );
		$customer_id = $order->get_customer_id();

		foreach ( $products as $product ) {
			if ( isset( $product['variation_id'] ) && ! empty( $product['variation_id'] ) ) {
				$groups_id = get_post_meta( $product['variation_id'], '_related_group', true );
			} else {
				$groups_id = get_post_meta( $product['product_id'], '_related_group', true );
			}

			// Update access to the groups
			if ( $groups_id && is_array( $groups_id ) ) {
				foreach ( $groups_id as $group_id ) {

					if ( $group_id > 0 ) {
						if ( empty( $customer_id ) || empty( $group_id ) ) {

							return;
						}

						self::update_add_group_access( $group_id, $customer_id, $order->get_id() );
						//delete user groups transient
						$transient_key = 'learndash_user_groups_' . $customer_id;
						delete_transient( $transient_key );

						//delete user courses transient
						$transient_key = 'learndash_user_courses_' . $customer_id;
						delete_transient( $transient_key );

						//delete group users transient
						$transient_key = 'learndash_group_users_' . $group_id;
						delete_transient( $transient_key );
					}
				}
			}
		}
	}

Scroll to Top