ulgm_refund_trash_group
Filters the group ID when refunding a trash group, allowing modification before deletion.
add_filter( 'ulgm_refund_trash_group', $callback, 10, 3 );
Description
This filter hook `ulgm_refund_trash_group` allows developers to control whether a license group is trashed when a WooCommerce order is refunded. It fires after an order refund is processed, enabling custom logic to prevent or modify the group trashing behavior based on order or group IDs. The hook returns `true` to proceed with trashing, or a different value to prevent it.
Usage
add_filter( 'ulgm_refund_trash_group', 'your_function_name', 10, 3 );
Parameters
-
$this(mixed) - This parameter represents the current object of the class where the filter is being applied.
-
$group_id(mixed) - This parameter refers to the current instance of the `WooCommerceLicenseRefund` class.
-
$order_id(mixed) - This parameter contains the ID of the license group associated with the order being refunded.
Return Value
The filtered value.
Examples
/**
* Example of how to filter the ulgm_refund_trash_group hook.
* This example demonstrates how to prevent a group from being trashed if it's marked as a "premium" group.
*
* @param bool $trash_group Whether to trash the group. Default is true.
* @param int $group_id The ID of the group to be trashed.
* @param int $order_id The ID of the WooCommerce order.
* @return bool Whether the group should be trashed.
*/
function my_custom_prevent_premium_group_trash( $trash_group, $group_id, $order_id ) {
// Check if the group has a custom meta key indicating it's a "premium" group.
// Replace 'is_premium_group' with your actual meta key.
$is_premium_group = get_post_meta( $group_id, 'is_premium_group', true );
if ( $is_premium_group === 'yes' ) {
// If it's a premium group, prevent it from being trashed.
// You might want to add an order note here to inform the admin.
$order = wc_get_order( $order_id );
if ( $order ) {
$group_title = get_the_title( $group_id );
$order->add_order_note( sprintf( esc_html__( 'Attempted to trash premium group #%1$d "%2$s" linked to order #%3$d, but it was prevented.', 'your-text-domain' ), $group_id, $group_title, $order_id ) );
}
return false; // Do not trash the group.
}
// Otherwise, allow the default behavior (trash the group).
return $trash_group;
}
add_filter( 'ulgm_refund_trash_group', 'my_custom_prevent_premium_group_trash', 10, 3 );
// Example of how to conditionally trash the group only if it's NOT a premium group.
// This is an alternative to the above and might be more directly aligned with the original intent if the filter parameter is meant to control deletion.
/**
* Example of how to filter the ulgm_refund_trash_group hook.
* This example demonstrates how to conditionally trash a group based on its "premium" status.
*
* @param bool $trash_group Whether to trash the group. Default is true.
* @param int $group_id The ID of the group to be trashed.
* @param int $order_id The ID of the WooCommerce order.
* @return bool Whether the group should be trashed.
*/
function my_custom_conditionally_trash_group( $trash_group, $group_id, $order_id ) {
// Check if the group has a custom meta key indicating it's a "premium" group.
// Replace 'is_premium_group' with your actual meta key.
$is_premium_group = get_post_meta( $group_id, 'is_premium_group', true );
// If it's a premium group, we don't want to trash it, so we force $trash_group to false.
if ( $is_premium_group === 'yes' ) {
return false;
}
// If it's not a premium group, return the original value of $trash_group.
return $trash_group;
}
add_filter( 'ulgm_refund_trash_group', 'my_custom_conditionally_trash_group', 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-license-refund.php:59
public function refund_trash_group( $order_id ) {
$order = wc_get_order( $order_id );
if ( ! $order instanceof WC_Order ) {
return;
}
$group_id = $order->get_meta( SharedFunctions::$linked_group_id_meta, true );
if ( empty( $group_id ) ) {
return;
}
if ( true !== apply_filters( 'ulgm_refund_trash_group', $this->trash_group_on_refund(), $group_id, $order_id ) ) {
return;
}
$data = array(
'ID' => $group_id,
'post_status' => 'draft',
);
wp_update_post( $data );
wp_trash_post( $group_id );
if ( true === apply_filters( 'ulgm_refund_trash_group_add_order_note', true, $order_id ) ) {
$group_title = get_the_title( $group_id );
$order->add_order_note(
sprintf( esc_html__( '#%1$d %2$s linked to the order was moved to trash after the refund.', 'uncanny-learndash-groups' ), $group_id, $group_title ), //phpcs:ignore WordPress.WP.I18n.MissingTranslatorsComment
false
);
}
}