Filter uncanny-learndash-groups

ulgm_new_license_product_args

Filters the arguments used when creating a new license product, triggered before the product is saved.

add_filter( 'ulgm_new_license_product_args', $callback, 10, 2 );

Description

Filters arguments for creating a new license product, allowing modification of post type, status, author, title, and content. Fires after determining the author for a new license product, before its creation. Developers can alter product details for custom integrations or behaviors.


Usage

add_filter( 'ulgm_new_license_product_args', 'your_function_name', 10, 2 );

Parameters

$user_id (mixed)
This parameter contains the ID of the user who will be set as the author of the new license product.
$_POST (mixed)
This parameter contains the ID of the user who will be set as the author of the new license product.

Return Value

The filtered value.


Examples

/**
 * Example function to modify the arguments for creating a new license product.
 *
 * This function demonstrates how to add a custom meta field to the product
 * arguments before it's created, for example, to store the group ID associated
 * with the license.
 *
 * @param array $product_args The default arguments for the new product.
 * @param int   $user_id      The ID of the user to be set as the post author.
 * @param array $post_data    The $_POST data received from the form.
 * @return array The modified product arguments.
 */
add_filter( 'ulgm_new_license_product_args', function( $product_args, $user_id, $post_data ) {

	// Check if the 'group_id' is present in the POST data.
	// This assumes the group ID is being submitted via a form.
	if ( isset( $post_data['group_id'] ) && is_numeric( $post_data['group_id'] ) ) {
		// Add a custom meta field to store the group ID.
		// The 'meta_input' key is used for adding meta fields during post creation.
		$product_args['meta_input']['_related_group_id'] = absint( $post_data['group_id'] );
	}

	// Optionally, you could modify other arguments like the title or content
	// based on the POST data or user ID. For instance, you might want to
	// include the group name in the content if it's available.
	if ( isset( $post_data['group_name'] ) && ! empty( $post_data['group_name'] ) ) {
		$product_args['post_content'] = sprintf( __( 'This license is for the group: %s', 'your-text-domain' ), sanitize_text_field( $post_data['group_name'] ) );
	}

	// Always return the modified arguments.
	return $product_args;

}, 10, 3 ); // Priority 10, accepts 3 arguments: $product_args, $user_id, $post_data

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-buy-courses.php:197

$user_id = array_shift( $admins );
			}
			if ( empty( $user_id ) || ! is_numeric( $user_id ) ) {
				$user_id = 1;
			}
		}

		$new_product_array = apply_filters(
			'ulgm_new_license_product_args',
			array(
				'post_type'      => 'product',
				'post_status'    => 'publish',
				'post_author'    => $user_id,
				'post_title'     => sprintf( __( '%s - License', 'uncanny-learndash-groups' ), $group_name ),
				'post_content'   => '',

Scroll to Top