Filter uncanny-learndash-groups

ulgm_license_is_sold_individually

Filters whether a product is sold individually when a license is involved, allowing customization of this core behavior.

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

Description

This filter hook determines if a license product is sold individually. Developers can use it to conditionally mark license products as non-individually sold, perhaps for bundle purchases. The hook passes the current sold individually status and the product object, allowing for dynamic control over product sales logic.


Usage

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

Parameters

$product (mixed)
This parameter holds the default value of the filter, which is `false`.

Return Value

The filtered value.


Examples

/**
 * Filter to control whether a license product is sold individually.
 *
 * By default, license products created by this plugin are NOT sold individually,
 * allowing multiple licenses to be purchased in a single order.
 * This filter allows for modification to enforce individual sales if needed.
 *
 * @param bool    $sold_individually Whether the product is sold individually.
 * @param WC_Product $product         The product object.
 * @return bool                      The updated sold individually status.
 */
add_filter( 'ulgm_license_is_sold_individually', function( $sold_individually, $product ) {

	// Example: If the product name contains "Single Use", force it to be sold individually.
	if ( strpos( $product->get_name(), 'Single Use' ) !== false ) {
		return true; // Force sold individually
	}

	// Example: If a custom meta key indicates "allow_multiple", don't sell individually.
	$allow_multiple = $product->get_meta( '_ulgm_allow_multiple_purchases' );
	if ( $allow_multiple === 'yes' ) {
		return false; // Do not sell individually
	}

	// Return the original value if no specific condition is met.
	return $sold_individually;

}, 10, 2 );

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

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