Filter uncanny-automator-pro

uap_option_woocommerce_stock_statuses

Filters the available WooCommerce stock statuses to allow custom modification before they are displayed or used.

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

Description

Filters the available WooCommerce stock status options for Uncanny Automator Pro. Developers can modify the presented stock status choices, such as adding custom statuses or removing existing ones, allowing for more granular control in recipe triggers and actions.


Usage

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

Parameters

$option (mixed)
This parameter holds the current value of the stock status option.

Return Value

The filtered value.


Examples

add_filter( 'uap_option_woocommerce_stock_statuses', 'my_custom_woocommerce_stock_statuses', 10, 1 );

/**
 * Customizes the available WooCommerce stock statuses for Uncanny Automator.
 *
 * This function allows developers to add, remove, or modify the stock statuses
 * that appear in Uncanny Automator's WooCommerce stock status trigger/action options.
 * For example, you might want to only show 'instock' and 'outofstock' and
 * exclude any custom statuses added by other plugins.
 *
 * @param array $option The array containing the stock status options for Uncanny Automator.
 *                      This array typically includes 'label', 'input_type', 'required', and 'options'.
 *                      The 'options' key holds an array of stock status slugs => labels.
 * @return array The modified array of stock status options.
 */
function my_custom_woocommerce_stock_statuses( $option ) {
    // Ensure we are dealing with the correct option structure
    if ( ! isset( $option['options'] ) || ! is_array( $option['options'] ) ) {
        return $option;
    }

    // Example: Filter out a specific custom stock status
    // Let's say we want to remove a hypothetical 'backorders_available' status.
    $statuses_to_remove = array( 'backorders_available' );

    foreach ( $statuses_to_remove as $status_slug ) {
        if ( array_key_exists( $status_slug, $option['options'] ) ) {
            unset( $option['options'][ $status_slug ] );
        }
    }

    // Example: Add a new custom stock status if it's not already present
    // This is less common for stock statuses, but demonstrates adding.
    // Let's assume a hypothetical 'limited_stock' status we want to add.
    $new_status_slug = 'limited_stock';
    $new_status_label = 'Limited Stock';

    if ( ! array_key_exists( $new_status_slug, $option['options'] ) ) {
        $option['options'][ $new_status_slug ] = $new_status_label;
    }

    // Return the modified array of options
    return $option;
}

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

uncanny-automator-pro/src/integrations/woocommerce/helpers/woocommerce-pro-helpers.php:1668

public function wc_stock_statuses( $label = null, $option_code = 'WCPRODSTOCKSTATUS', $is_any = false ) {

		if ( ! $label ) {
			$label = esc_html_x( 'Product stock status', 'Woocommerce', 'uncanny-automator-pro' );
		}

		$status = array();
		if ( true === $is_any ) {
			$status['-1'] = esc_html_x( 'Any status', 'WooCommerce', 'uncanny-automator-pro' );
		}

		$options = array(
			'onbackorder' => esc_html_x( 'On backorder', 'Woocommerce', 'uncanny-automator-pro' ),
			'instock'     => esc_html_x( 'In stock', 'Woocommerce', 'uncanny-automator-pro' ),
			'outofstock'  => esc_html_x( 'Out of stock', 'Woocommerce', 'uncanny-automator-pro' ),
		);

		$status = $status + $options;

		$option = array(
			'option_code'     => $option_code,
			'label'           => $label,
			'input_type'      => 'select',
			'required'        => true,
			'options'         => $status,
			'relevant_tokens' => array(),
		);

		return apply_filters( 'uap_option_woocommerce_stock_statuses', $option );
	}

Scroll to Top