Filter uncanny-learndash-groups

ulgm_bulk_discount_exclude_license_ids

Filters the license IDs excluded from bulk discount calculations.

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

Description

Filter to exclude specific license IDs from bulk discount calculations. Use this hook to conditionally prevent licenses from contributing to or receiving bulk discounts based on their ID. Accepts an array of license IDs and the product ID.


Usage

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

Parameters

$product_id (mixed)
This parameter is an array that likely holds license IDs that should be excluded from bulk discounts.

Return Value

The filtered value.


Examples

/**
 * Example: Exclude specific license IDs from bulk discount calculations for a particular product.
 *
 * This filter allows you to prevent bulk discounts from applying to products
 * that have specific license IDs associated with them. This could be useful
 * if certain licenses have different pricing structures or are not intended
 * for bulk discounts.
 *
 * @param array $excluded_license_ids An array of license IDs to exclude.
 * @param int   $product_id           The ID of the product being checked for discounts.
 *
 * @return array The modified array of license IDs to exclude.
 */
add_filter( 'ulgm_bulk_discount_exclude_license_ids', function ( $excluded_license_ids, $product_id ) {

	// Define a list of license IDs that should be excluded from bulk discounts.
	// For demonstration purposes, let's say license IDs 101 and 105 are always excluded.
	$always_excluded_licenses = array( 101, 105 );

	// If the current product ID is, for example, product ID 50,
	// and we also want to exclude license ID 200 specifically for this product.
	if ( 50 === $product_id ) {
		$excluded_license_ids[] = 200;
	}

	// Merge the always excluded licenses with any product-specific exclusions.
	// Ensure no duplicates are added.
	$excluded_license_ids = array_unique( array_merge( $excluded_license_ids, $always_excluded_licenses ) );

	// Return the updated array of excluded license IDs.
	return $excluded_license_ids;
}, 10, 2 ); // Priority 10, accepts 2 arguments

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-bulk-discount.php:386

protected function gather_discount_coeffs() {
		//
		$quantities = array();
		$quantity   = WC()->cart->get_cart_item_quantities();

		////
		$user_id       = wp_get_current_user()->ID;
		$get_transient = SharedFunctions::get_transient_cache( '_ulgm_user_USERID_order', $user_id );
		if ( is_user_logged_in() && ! empty( $get_transient ) ) {
			$product = wc_get_product( absint( $get_transient['order_details']['product_id'] ) );
			if ( SharedFunctions::is_group_licensed_product( $product ) ) {
				$quantities[ $product->get_id() ][] = absint( $get_transient['existing_qty'] );
			}
		}

		if ( $quantity ) {
			foreach ( $quantity as $product_id => $qty ) {
				$product = wc_get_product( $product_id );
				if ( SharedFunctions::is_group_licensed_product( $product ) ) {
					$quantities[ $product_id ][] = $qty;
				}
			}
		}

		foreach ( $quantities as $product_id => $qty ) {
			$excluded_licenses = apply_filters( 'ulgm_bulk_discount_exclude_license_ids', array(), $product_id );
			$excluded_licenses = array_map( 'absint', $excluded_licenses );
			if ( is_array( $excluded_licenses ) && ! empty( $excluded_licenses ) && in_array( $product_id, $excluded_licenses, true ) ) {
				continue;
			}
			if ( 0 !== absint( $product_id ) && ! array_key_exists( $product_id, $this->discount_coeffs ) ) {
				$total_qty                                          = absint( array_sum( $quantities[ $product_id ] ) );
				$this->discount_coeffs[ $product_id ]['coeff']      = $this->get_discounted_coeff( $total_qty );
				$this->discount_coeffs[ $product_id ]['orig_price'] = SharedFunctions::get_custom_product_price( wc_get_product( $product_id ) );
				$this->discount_coeffs[ $product_id ]['quantity']   = $total_qty;
			}
		}
	}

Scroll to Top