Filter Uncanny Redemption Codes

ulc_woocommerce_allowed_order_statuses_for_codes

Filters allowed WooCommerce order statuses for codes, letting you customize which statuses are recognized.

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

Description

Filters the allowed WooCommerce order statuses for code usage. Developers can modify this array to include or exclude specific order statuses, controlling when order codes are generated or processed based on the order's current state. This hook fires before retrieving or processing order codes.


Usage

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

Parameters

$order (mixed)
This parameter contains an array of WooCommerce order statuses that are considered valid for using automation codes.

Return Value

The filtered value.


Examples

<?php
/**
 * Example of adding a filter to 'ulc_woocommerce_allowed_order_statuses_for_codes'.
 *
 * This filter allows developers to modify the list of WooCommerce order statuses
 * that are considered "allowed" for certain code operations within the Ultimate WooCommerce Codes plugin.
 * For instance, you might want to exclude 'processing' if your codes should only be redeemed
 * after an order is fully completed and shipped.
 *
 * @param array $allowed_statuses The default array of allowed order statuses.
 * @param WC_Order $order The WooCommerce order object.
 * @return array The modified array of allowed order statuses.
 */
add_filter( 'ulc_woocommerce_allowed_order_statuses_for_codes', function( $allowed_statuses, $order ) {

	// Let's say we want to exclude 'processing' if the order has a specific meta key set,
	// indicating a special type of order where codes should only be redeemed upon completion.
	if ( $order instanceof WC_Order && $order->get_meta( '_special_delivery_order', true ) === 'yes' ) {
		// Remove 'processing' from the allowed statuses if the meta key is set.
		if ( ( $key = array_search( 'processing', $allowed_statuses, true ) ) !== false ) {
			unset( $allowed_statuses[ $key ] );
		}
	}

	// You might also want to add a new status if your custom order status is available.
	// For example, if you have a custom status like 'delivered_and_checked'.
	// Make sure to replace 'delivered_and_checked' with your actual custom status slug.
	// if ( $order instanceof WC_Order && in_array( 'delivered_and_checked', array_keys( wc_get_order_statuses() ) ) ) {
	// 	$allowed_statuses[] = 'delivered_and_checked';
	// }


	// Always return the modified array of statuses.
	return $allowed_statuses;
}, 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/integrations/woocommerce/class-woo-automator-codes.php:652
src/classes/integrations/woocommerce/class-woo-automator-codes.php:842

return;
		}

		$order_codes = Database::get_codes_usage_by_order_id( $order_id, false );
		if ( empty( $order_codes ) ) {
			return;
		}
		$statuses = apply_filters(
			'ulc_woocommerce_allowed_order_statuses_for_codes',
			array(
				'processing',
				'completed',
			),
			$order
		);

Scroll to Top