Filter
uncanny-automator
uap_option_all_studiocart_products
Filters the all studiocart products option.
add_filter( 'uap_option_all_studiocart_products', $callback, 10, 1 );
Description
This filter allows developers to modify the array of StudioCart product options available for selection. It fires when the available products are being prepared for use in automations, enabling customization of the product list displayed to users. Developers can filter the `$option` array to add, remove, or alter product data.
Usage
add_filter( 'uap_option_all_studiocart_products', 'your_function_name', 10, 1 );
Parameters
-
$option(mixed) - This parameter holds the current value of the `$option` being filtered, which likely represents a collection or data structure related to StudioCart products.
Return Value
The filtered value.
Examples
/**
* Example: Filter StudioCart products to exclude those with a specific tag.
*
* This filter allows you to modify the list of StudioCart products returned
* by the 'uap_option_all_studiocart_products' hook.
* In this example, we'll remove products that are tagged with 'internal-only'.
*/
add_filter( 'uap_option_all_studiocart_products', function( $option ) {
// Ensure $option is an array before proceeding.
if ( ! is_array( $option ) ) {
return $option;
}
// Get the list of StudioCart products.
$products = $option['options'];
// Get all terms for the 'product_tag' taxonomy.
$tags = get_terms( array(
'taxonomy' => 'product_tag',
'hide_empty' => false,
) );
$internal_tag_id = false;
if ( ! empty( $tags ) && ! is_wp_error( $tags ) ) {
foreach ( $tags as $tag ) {
// Find the ID of the 'internal-only' tag.
if ( $tag->slug === 'internal-only' ) {
$internal_tag_id = $tag->term_id;
break;
}
}
}
// If the 'internal-only' tag was found, filter the products.
if ( $internal_tag_id ) {
$filtered_products = array_filter( $products, function( $product ) use ( $internal_tag_id ) {
// Check if the product has the 'internal-only' tag.
if ( isset( $product['terms']['product_tag'] ) && in_array( $internal_tag_id, $product['terms']['product_tag'] ) ) {
// Exclude this product.
return false;
}
// Include this product.
return true;
} );
$option['options'] = array_values( $filtered_products ); // Re-index array
}
return $option;
}, 10, 1 ); // Priority 10, accepts 1 argument
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/studiocart/helpers/studiocart-helpers.php:88
src/integrations/studiocart/helpers/studiocart-helpers.php:112
public function all_products( $label = null, $option_code = 'STUDIOCARTPRODUCTS', $args = array() ) {
$label = null === $label ? esc_attr__( 'Product', 'uncanny-automator' ) : $label;
$args = wp_parse_args(
$args,
array(
'uo_include_any' => false,
'uo_any_label' => esc_attr__( 'Any product', 'uncanny-automator' ),
)
);
$token = key_exists( 'token', $args ) ? $args['token'] : false;
$is_ajax = key_exists( 'is_ajax', $args ) ? $args['is_ajax'] : false;
$target_field = key_exists( 'target_field', $args ) ? $args['target_field'] : '';
$end_point = key_exists( 'endpoint', $args ) ? $args['endpoint'] : '';
$options = array();
$option = array(
'option_code' => $option_code,
'label' => $label,
'input_type' => 'select',
'required' => true,
'supports_tokens' => $token,
'is_ajax' => $is_ajax,
'fill_values_in' => $target_field,
'endpoint' => $end_point,
'options' => $options,
);
if ( ! Automator()->helpers->recipe->load_helpers ) {
return apply_filters( 'uap_option_all_studiocart_products', $option );
}
$args = array(
'post_type' => 'sc_product',
'posts_per_page' => 999, // phpcs:ignore WordPress.WP.PostsPerPage.posts_per_page_posts_per_page
'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' => array(),
);
$option['options'] = $options;
return apply_filters( 'uap_option_all_studiocart_products', $option );
}