Filter uncanny-learndash-groups

ulgm_refund_trash_group_add_order_note

Filters whether to add an order note when refunding a group from trash.

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

Description

Filters whether to trash a license group when an order is trashed. Allows developers to conditionally prevent or modify the behavior. Fires after checking if the order is valid and if a group is linked. Defaults to true, enabling the trashing action.


Usage

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

Parameters

$order_id (mixed)
This parameter indicates whether the refund process for the license group should be continued.

Return Value

The filtered value.


Examples

/**
 * Example of using the ulgm_refund_trash_group_add_order_note filter.
 * This example prevents adding an order note when the group ID is 0,
 * assuming 0 might indicate a special case or an error.
 *
 * @param bool     $should_add_note Whether to add the order note.
 * @param int|null $order_id        The ID of the WooCommerce order.
 *
 * @return bool The modified value of $should_add_note.
 */
add_filter( 'ulgm_refund_trash_group_add_order_note', function ( $should_add_note, $order_id ) {

	// If the order ID is not valid, don't proceed.
	if ( ! $order_id ) {
		return false; // Do not add the note if order_id is invalid.
	}

	// Get the order object to retrieve meta data.
	$order = wc_get_order( $order_id );

	// If the order object is not valid, don't proceed.
	if ( ! $order instanceof WC_Order ) {
		return false; // Do not add the note if the order is invalid.
	}

	// Retrieve the linked group ID from order meta.
	// Replace 'your_shared_functions_class' with the actual class name if different.
	// Replace 'linked_group_id_meta' with the actual meta key if different.
	$group_id = $order->get_meta( 'your_shared_functions_class::linked_group_id_meta', true );

	// If the group ID is 0, it might indicate a scenario where we don't want to add a note.
	if ( $group_id === 0 ) {
		return false; // Prevent adding the note for group ID 0.
	}

	// Otherwise, allow the default behavior.
	return $should_add_note;

}, 10, 2 ); // Priority 10, 2 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-license-refund.php:72

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
			);
		}
	}


Scroll to Top