Filter
uncanny-automator
uap_option_all_memberpress_products_onetime
Filters the list of one-time MemberPress products available for selection.
add_filter( 'uap_option_all_memberpress_products_onetime', $callback, 10, 1 );
Description
Filters the options array for displaying all MemberPress one-time products. Developers can modify the product selection options before they are displayed. This hook fires when generating the form field for MemberPress one-time products.
Usage
add_filter( 'uap_option_all_memberpress_products_onetime', 'your_function_name', 10, 1 );
Parameters
-
$option(mixed) - This parameter is an optional label that can be used to customize the display name of the product option.
Return Value
The filtered value.
Examples
add_filter( 'uap_option_all_memberpress_products_onetime', function( $option ) {
// This example demonstrates how to modify the list of MemberPress one-time products.
// We'll assume $option is an array containing product data.
// Here, we're adding a custom product to the list if it doesn't already exist.
// Simulate a custom product to be added.
$custom_product = array(
'id' => 999, // A hypothetical unique ID for our custom product
'label' => 'My Custom One-Time Product',
'value' => 'custom_999', // A unique identifier for the option
);
// Check if our custom product is already in the options.
$product_exists = false;
if ( isset( $option['options'] ) && is_array( $option['options'] ) ) {
foreach ( $option['options'] as $existing_product ) {
if ( isset( $existing_product['value'] ) && $existing_product['value'] === $custom_product['value'] ) {
$product_exists = true;
break;
}
}
}
// If the custom product doesn't exist, add it to the beginning of the options array.
if ( ! $product_exists ) {
// Ensure 'options' key exists and is an array.
if ( ! isset( $option['options'] ) || ! is_array( $option['options'] ) ) {
$option['options'] = array();
}
// Add the custom product to the start of the array.
array_unshift( $option['options'], $custom_product );
}
// You could also remove products, modify their labels, or filter based on other criteria.
// For example, to remove a specific product if its value is 'some_product_value':
/*
$option['options'] = array_filter( $option['options'], function( $product ) {
return $product['value'] !== 'some_product_value';
});
*/
// Always return the modified $option array.
return $option;
}, 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/memberpress/helpers/memberpress-helpers.php:179
public function all_memberpress_products_onetime( $label = null, $option_code = 'MPPRODUCT', $args = array() ) {
if ( ! $label ) {
$label = esc_attr__( 'Product', 'uncanny-automator' );
}
$args = wp_parse_args(
$args,
array(
'uo_include_any' => false,
'uo_any_label' => esc_attr__( 'Any one-time subscription product', 'uncanny-automator' ),
)
);
$options = array();
if ( $args['uo_include_any'] ) {
$options['-1'] = $args['uo_any_label'];
}
//$posts = get_posts( );
$query_args = array(
'post_type' => 'memberpressproduct',
'posts_per_page' => 999,
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => '_mepr_product_period_type',
'value' => 'lifetime',
'compare' => '=',
),
),
);
$results = Automator()->helpers->recipe->wp_query( $query_args );
if ( ! empty( $results ) ) {
foreach ( $results as $k => $v ) {
$options[ $k ] = $v;
}
}
$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' ),
);
if ( isset( $args['relevant_tokens'] ) && ! empty( $args['relevant_tokens'] ) && is_array( $args['relevant_tokens'] ) ) {
$relevant_tokens = array_merge( $relevant_tokens, $args['relevant_tokens'] );
}
$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_memberpress_products_onetime', $option );
}