uap_option_all_upsell_products
Filters the entire list of upsell products before they are displayed, allowing for customization of the upsell product selection.
add_filter( 'uap_option_all_upsell_products', $callback, 10, 1 );
Description
Filter the list of all available upsell product options provided by the Upsell plugin. Developers can use this hook to add, remove, or modify the options presented within Uncanny Automator's upsell triggers and actions, allowing for custom product integrations. This filter operates when the upsell plugin's options are being retrieved.
Usage
add_filter( 'uap_option_all_upsell_products', 'your_function_name', 10, 1 );
Parameters
-
$option(mixed) - This parameter represents the current value of the upsell product option, which can be modified by the filter.
Return Value
The filtered value.
Examples
/**
* Example of how to filter the 'uap_option_all_upsell_products' hook.
*
* This example demonstrates how to conditionally add or modify options
* available for Uncanny Automator's Upsell Products integration.
*
* For instance, you might want to add a custom field related to upsell products
* if a specific condition is met, or remove certain default options if they
* are not relevant to your integration.
*
* @param array $option The array of options for upsell products.
*
* @return array The modified array of options.
*/
add_filter( 'uap_option_all_upsell_products', 'my_custom_uap_upsell_products_options', 10, 1 );
function my_custom_uap_upsell_products_options( $option ) {
// Check if the current user has a specific capability or if a certain plugin is active.
// Replace with your actual conditional logic.
if ( current_user_can( 'manage_options' ) && class_exists( 'My_Custom_Upsell_Plugin' ) ) {
// Example: Add a new custom option if the condition is met.
// This assumes $option['options'] is an array where keys are option codes and values are human-readable labels.
// Let's assume '$option_code' is a variable available in the original context or defined here.
// For this example, we'll use a placeholder. In a real scenario, $option_code might be dynamic.
$custom_option_code = 'MY_CUSTOM_FIELD';
$option['options'][ $custom_option_code ] = esc_attr__( 'My Custom Upsell Data', 'uncanny-automator' );
// Example: Modify an existing option label.
if ( isset( $option['options']['some_other_option_CODE'] ) ) {
$option['options']['some_other_option_CODE'] = esc_attr__( 'Modified Label for Some Option', 'uncanny-automator' );
}
}
// Example: Remove an option if it's not needed.
// Make sure the key exists before trying to unset it.
if ( isset( $option['options']['_THUMB_URL'] ) ) {
// Let's say we don't want to expose the thumbnail URL directly for some reason.
// unset( $option['options']['_THUMB_URL'] );
// Note: In this specific case, the original code snippet shows it's added within a sub-array,
// so you'd need to adjust the access path accordingly if modifying or removing it.
// The provided snippet suggests it's part of a larger structure, so adjust as needed.
// For demonstration, let's assume a flat structure for this example.
}
// It's crucial to return the modified $option array.
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
src/integrations/upsell-plugin/helpers/upsell-plugin-helpers.php:88
uncanny-automator-pro/src/integrations/upsell-plugin/helpers/upsell-plugin-pro-helpers.php:74
public function all_upsell_products( $label = null, $option_code = 'USPRODUCT' ) {
if ( ! $label ) {
$label = esc_attr__( 'Product', 'uncanny-automator' );
}
$args = array(
'post_type' => 'upsell_product',
'posts_per_page' => 999,
'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_code => esc_attr__( 'Product title', 'uncanny-automator' ),
$option_code . '_ID' => esc_attr__( 'Product ID', 'uncanny-automator' ),
$option_code . '_URL' => esc_attr__( 'Product URL', 'uncanny-automator' ),
$option_code . '_THUMB_ID' => esc_attr__( 'Product featured image ID', 'uncanny-automator' ),
$option_code . '_THUMB_URL' => esc_attr__( 'Product featured image URL', 'uncanny-automator' ),
),
);
return apply_filters( 'uap_option_all_upsell_products', $option );
}