Filter uncanny-learndash-groups

ulgm_license_category_ids

Filters the license category IDs used for a specific product during core plugin operations.

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

Description

Filter the array of category IDs to associate with a new license product. Developers can modify these IDs to assign the license to different product categories within WooCommerce, influencing how it's displayed and organized.


Usage

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

Parameters

$args (mixed)
This parameter contains the arguments used to create a new license product.
$product (mixed)
This parameter is an array containing arguments used to create a new WooCommerce product, including its title and other relevant data.

Return Value

The filtered value.


Examples

add_filter( 'ulgm_license_category_ids', 'my_custom_license_category_ids', 10, 2 );

/**
 * Example function to modify the category IDs for a license product.
 *
 * This function demonstrates how to add a specific category to the license product
 * if it doesn't already exist, or replace existing categories with a predefined set.
 *
 * @param array    $category_ids The current array of category IDs.
 * @param WC_Product $product The product object being created.
 * @return array The modified array of category IDs.
 */
function my_custom_license_category_ids( $category_ids, $product ) {
	// Define a specific category ID to always associate with license products.
	// Replace '123' with the actual ID of your desired category.
	$my_license_category_id = 123;

	// Ensure the custom category ID is included.
	if ( ! in_array( $my_license_category_id, $category_ids ) ) {
		$category_ids[] = $my_license_category_id;
	}

	// Optional: If you want to enforce a specific set of categories, you could do something like this:
	// $category_ids = array( $my_license_category_id, 456 ); // Replace 456 with another category ID if needed.

	// Remove duplicates in case the original $category_ids already contained the custom ID.
	$category_ids = array_unique( $category_ids );

	// You can also check product properties to conditionally add categories.
	// For example, if the product has a specific meta key set:
	// if ( $product->get_meta( '_is_premium_license', true ) ) {
	//     $premium_category_id = 789; // Replace with your premium category ID
	//     if ( ! in_array( $premium_category_id, $category_ids ) ) {
	//         $category_ids[] = $premium_category_id;
	//         $category_ids = array_unique( $category_ids );
	//     }
	// }

	return $category_ids;
}

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:360

public static function create_license_product( $args ) {

		// Get an empty instance of the product object (defining it's type)
		$product = new WC_Product_License();
		if ( ! $product ) {
			return false;
		}

		// Product name (Title) and slug
		$product->set_name( $args['post_title'] ); // Name (title).
		// Status ('publish', 'pending', 'draft' or 'trash')
		$product->set_status( isset( $args['post_status'] ) ? $args['post_status'] : 'publish' );
		// Visibility ('hidden', 'visible', 'search' or 'catalog')
		$product->set_catalog_visibility( 'hidden' );
		// Featured (boolean)
		$product->set_featured( isset( $args['featured'] ) ? $args['featured'] : false );
		// Virtual (boolean)
		$product->set_virtual( apply_filters( 'ulgm_license_is_virtual', true, $product ) );
		// Downloadable (boolean)
		$product->set_downloadable( apply_filters( 'ulgm_license_is_downloadable', false, $product ) );
		// Sold Individually
		$product->set_sold_individually( apply_filters( 'ulgm_license_is_sold_individually', false, $product ) );
		// Reviews
		$product->set_reviews_allowed( apply_filters( 'ulgm_license_is_reviews_allowed', false, $product ) );

		// Taxes
		if ( get_option( 'woocommerce_calc_taxes' ) === 'yes' ) {
			$product->set_tax_status( isset( $args['tax_status'] ) ? $args['tax_status'] : 'taxable' );
			$product->set_tax_class( isset( $args['tax_class'] ) ? $args['tax_class'] : '' );
		}

		// Attributes et default attributes
		if ( isset( $args['attributes'] ) ) {
			$product->set_attributes( self::wc_prepare_product_attributes( apply_filters( 'ulgm_license_attributes', $args['attributes'], $product ) ) );
		}
		if ( isset( $args['default_attributes'] ) ) {
			$product->set_default_attributes( apply_filters( 'ulgm_license_default_attributes', $args['default_attributes'], $product ) );
		} // Needs a special formatting

		// Product categories and Tags
		if ( isset( $args['category_ids'] ) ) {
			$product->set_category_ids( apply_filters( 'ulgm_license_category_ids', array( $args['category_ids'] ), $product ) );
		}

		if ( isset( $args['tag_ids'] ) ) {
			$product->set_tag_ids( apply_filters( 'ulgm_license_tag_ids', array( $args['tag_ids'] ), $product ) );
		}

		if ( 'yes' === get_option( 'woocommerce_calc_taxes' ) ) {
			$tax_status = get_option( 'ulgm_group_license_tax_status', '' );
			$tax_class  = get_option( 'ulgm_group_license_tax_class', '' );

			if ( ! empty( $tax_class ) ) {
				$product->set_tax_class( $tax_class );
			}

			if ( ! empty( $tax_status ) ) {
				$product->set_tax_status( $tax_status );
			}
		}

		## --- SAVE PRODUCT --- ##
		$product->save();

		//Set price
		$get_price = SharedFunctions::get_custom_product_price( $product, true );
		$product->set_price( $get_price );
		$product->set_regular_price( $get_price );

		return $product;
	}


Scroll to Top