Filter
uncanny-automator
automator_wc_product_search_limit
Filters the maximum number of results returned for product searches within WooCommerce automations.
add_filter( 'automator_wc_product_search_limit', $callback, 10, 1 );
Description
Filters the number of WooCommerce products returned in search results for automations. Developers can adjust this limit to control how many products are displayed or processed by the plugin. The default is 50.
Usage
add_filter( 'automator_wc_product_search_limit', 'your_function_name', 10, 1 );
Return Value
The filtered value.
Examples
/**
* Filter the maximum number of products to return in an automated search.
*
* This example demonstrates how to reduce the product search limit to 20
* when the search term is a number, suggesting a specific product ID is being searched.
* Otherwise, it defaults to the original value.
*
* @param int $limit The current product search limit. Defaults to 50.
* @return int The new product search limit.
*/
add_filter( 'automator_wc_product_search_limit', function( $limit ) {
// Check if the search term is numeric, indicating a potential product ID search.
if ( isset( $_POST['q'] ) && is_numeric( $_POST['q'] ) ) {
// If searching by ID, we might want a more specific result, so reduce the limit.
return 20;
}
// Otherwise, return the original limit.
return $limit;
}, 10, 1 );
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:57
public function search_wc_products_for_trigger() {
Automator()->utilities->ajax_auth_check();
$search_term = isset( $_POST['q'] ) ? sanitize_text_field( wp_unslash( $_POST['q'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => apply_filters( 'automator_wc_product_search_limit', 50 ),
'orderby' => 'title',
'order' => 'ASC',
's' => $search_term,
);
// Allow searching by product ID directly.
if ( is_numeric( $search_term ) ) {
$args['post__in'] = array( absint( $search_term ) );
unset( $args['s'] );
}
$products = get_posts( $args );
$options = array();
$options[] = array(
'value' => '-1',
'text' => __( 'Any product', 'uncanny-automator' ),
);
foreach ( $products as $product ) {
$title = ! empty( $product->post_title )
? $product->post_title
: sprintf( /* translators: %d is the product ID */ __( 'ID: %d (no title)', 'uncanny-automator' ), $product->ID );
$options[] = array(
'value' => $product->ID,
'text' => $title,
);
}
echo wp_json_encode(
array(
'success' => true,
'options' => $options,
)
);
wp_die();
}