Filter uncanny-learndash-groups

ulgm_license_tag_ids

Filters the license tag IDs for a product, allowing modification before they are applied or retrieved.

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

Description

Filters the license tag IDs used for creating a WooCommerce product. Developers can modify these IDs to associate custom tags with new license products, influencing product categorization and filtering. The $args parameter contains the product arguments, and $product is the WC_Product_License object being created.


Usage

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

Parameters

$args (mixed)
This parameter contains an array of arguments used to create a new WooCommerce product.
$product (mixed)
This parameter contains an array of arguments used to create or update a WooCommerce product.

Return Value

The filtered value.


Examples

<?php
/**
 * Example of filtering the 'ulgm_license_tag_ids' hook to add a specific tag to license products.
 *
 * This function demonstrates how to hook into the 'ulgm_license_tag_ids' filter.
 * It checks if the provided product is a license type and, if so, appends a
 * predefined tag ID to the list of tag IDs associated with the product.
 *
 * @param array  $tag_ids The current array of tag IDs.
 * @param WC_Product $product The WooCommerce product object being created or updated.
 * @return array The modified array of tag IDs.
 */
add_filter(
	'ulgm_license_tag_ids',
	function( $tag_ids, $product ) {
		// Ensure $tag_ids is an array, even if it's initially empty or null.
		if ( ! is_array( $tag_ids ) ) {
			$tag_ids = array();
		}

		// Define the tag ID you want to consistently add to license products.
		// Replace '123' with the actual ID of your desired tag.
		$new_license_tag_id = 123;

		// Check if the tag ID already exists in the array to avoid duplicates.
		if ( ! in_array( $new_license_tag_id, $tag_ids ) ) {
			// Add the new tag ID to the array.
			$tag_ids[] = $new_license_tag_id;
		}

		// Return the modified array of tag IDs.
		return $tag_ids;
	},
	10, // Priority: Default priority.
	2   // Accepted Args: The filter provides $tag_ids and $product.
);

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

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