uap_option_all_wc_products
Filters the list of all WooCommerce products available for selection within the plugin settings.
add_filter( 'uap_option_all_wc_products', $callback, 10, 1 );
Description
Filters the list of all WooCommerce products used in Uncanny Automator. Developers can modify this array to exclude specific products or add custom ones, enabling fine-grained control over product availability within automations. Ensure the returned array maintains the expected key-value format.
Usage
add_filter( 'uap_option_all_wc_products', 'your_function_name', 10, 1 );
Parameters
-
$option(mixed) - This parameter holds an array of WooCommerce products.
Return Value
The filtered value.
Examples
<?php
/**
* Example of how to use the 'uap_option_all_wc_products' filter hook.
* This filter allows you to modify the array of WooCommerce products
* before they are presented in a Uncanny Automator Pro trigger or action.
*
* In this example, we'll filter out any WooCommerce products that are
* marked as "Virtual" to only show physical products.
*/
add_filter(
'uap_option_all_wc_products',
function( $wc_products_option ) {
// Check if the $wc_products_option is in the expected format
// (an array containing an 'options' key which is an array of product data).
if ( ! is_array( $wc_products_option ) || ! isset( $wc_products_option['options'] ) || ! is_array( $wc_products_option['options'] ) ) {
return $wc_products_option; // Return original if format is unexpected
}
$filtered_products = array();
foreach ( $wc_products_option['options'] as $product_id => $product_data ) {
// Assuming product_data contains information about the product,
// including its type. This is a simplified assumption; in a real
// scenario, you might need to fetch more product details via WC API.
// WooCommerce product objects have a get_virtual() method.
// We need to load the WC_Product object to access its methods.
$product = wc_get_product( $product_id );
// If product loading fails or the product is virtual, skip it.
if ( ! $product || $product->get_virtual() ) {
continue;
}
// Keep the product if it's not virtual.
$filtered_products[ $product_id ] = $product_data;
}
// Update the 'options' key with our filtered list of products.
$wc_products_option['options'] = $filtered_products;
// You might also want to adjust the 'label' or 'relevant_tokens'
// if your filtering significantly changes the available options.
// For this example, we'll keep them as is.
return $wc_products_option;
},
10, // Priority: Higher number means later execution. 10 is default.
1 // Accepted arguments: This filter only passes one argument ($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
src/integrations/woocommerce/helpers/woocommerce-helpers.php:231
uncanny-automator-pro/src/integrations/woocommerce/helpers/woocommerce-pro-helpers.php:243
uncanny-automator-pro/src/integrations/woocommerce/helpers/woocommerce-pro-helpers.php:1119
uncanny-automator-pro/src/integrations/wholesale-suite/helpers/wholesale-suite-pro-helpers.php:55
public function load_products( $label = null, $option_code = 'WOOPRODUCT', $relevant_tokens = array() ) {
if ( ! $label ) {
$label = esc_attr__( 'Product', 'uncanny-automator' );
}
$args = array(
'post_type' => 'product',
'posts_per_page' => 999999,
'orderby' => 'title',
'order' => 'ASC',
'post_status' => 'publish',
);
$options = Automator()->helpers->recipe->options->wp_query( $args, true, esc_attr__( 'Any product', 'uncanny-automator' ) );
$option = array(
'option_code' => $option_code,
'label' => $label,
'input_type' => 'select',
'required' => true,
'options' => $options,
'relevant_tokens' => $relevant_tokens,
);
return apply_filters( 'uap_option_all_wc_products', $option );
}