ld_woocommerce_remove_subscription_group_access
Filters whether to remove subscription group access for a WooCommerce order.
add_filter( 'ld_woocommerce_remove_subscription_group_access', $callback, 10, 1 );
Description
Filters whether to remove group access for a WooCommerce subscription order. Return `false` to prevent access removal. Useful for custom logic controlling group access based on order details or subscription status. Fires after subscription renewal and before group access is potentially revoked.
Usage
add_filter( 'ld_woocommerce_remove_subscription_group_access', 'your_function_name', 10, 1 );
Parameters
-
$order(mixed) - This parameter is a boolean that controls whether the function should proceed with removing subscription group access.
Return Value
The filtered value.
Examples
/**
* Example of using the 'ld_woocommerce_remove_subscription_group_access' filter hook.
* This function demonstrates how to conditionally prevent LearnDash group access removal
* for specific WooCommerce subscriptions based on custom product meta.
*
* @param mixed $return_value The default return value (true to proceed).
* @param WC_Order $order The WooCommerce order object.
* @param string $hook_name The name of the hook being filtered.
*
* @return bool Returns false to stop the group access removal, true to allow it.
*/
add_filter(
'ld_woocommerce_remove_subscription_group_access',
function ( $return_value, $order, $hook_name ) {
// Define a custom product ID that we want to exempt from group access removal.
$exempt_product_id = 12345; // Replace with a real product ID
// Check if the order contains the exempt product.
$contains_exempt_product = false;
foreach ( $order->get_items() as $item_id => $item ) {
$product = $item->get_product();
if ( $product && $product->get_id() === $exempt_product_id ) {
$contains_exempt_product = true;
break;
}
}
// If the order contains the exempt product, prevent group access removal.
if ( $contains_exempt_product ) {
// Optionally, you could log this action or trigger a different process.
error_log( 'Preventing LearnDash group access removal for order ID: ' . $order->get_id() . ' due to exempt product.' );
return false; // Return false to stop the default group access removal process.
}
// Otherwise, allow the default group access removal to proceed.
return $return_value;
},
10,
3 // Number of accepted arguments: $return_value, $order, $hook_name
);
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:678
public static function remove_subscription_group_access( $order ) {
if ( ! apply_filters( 'ld_woocommerce_remove_subscription_group_access', true, $order, current_filter() ) ) {
return;
}
// Get products related to this order
$products = $order->get_items();
foreach ( $products as $product ) {
$groups_id = get_post_meta( $product['product_id'], '_related_group', true );
// Update access to the groups
if ( isset( $groups_id ) && is_array( $groups_id ) ) {
foreach ( $groups_id as $group_id ) {
if ( is_array( $group_id ) && ! empty( $group_id ) ) {
foreach ( $group_id as $g___id ) {
self::update_remove_group_access( $g___id, $order->get_customer_id(), $order->get_id() );
}
} elseif ( is_numeric( $group_id ) ) {
self::update_remove_group_access( $group_id, $order->get_customer_id(), $order->get_id() );
}
foreach ( $order->get_related_orders() as $o_id ) {
self::update_remove_group_access( $group_id, $order->get_customer_id(), $o_id );
//delete user groups transient
$transient_key = 'learndash_user_groups_' . $order->get_customer_id();
delete_transient( $transient_key );
//delete user courses transient
$transient_key = 'learndash_user_courses_' . $order->get_customer_id();
delete_transient( $transient_key );
//delete group users transient
$transient_key = 'learndash_group_users_' . $group_id;
delete_transient( $transient_key );
}
}
}
}
}