uap_option_all_wc_variable_products
Filters all WooCommerce variable products to control their display and availability within the UC WooCommerce Memberships integration.
add_filter( 'uap_option_all_wc_variable_products', $callback, 10, 1 );
Description
Filters the options array for displaying WooCommerce variable product tokens. Developers can modify the `$option` array to add or remove relevant tokens, controlling which WooCommerce variable product details are available for use in Uncanny Automator triggers and actions. This hook fires when setting up the available tokens for variable products within the Uncanny Automator integration.
Usage
add_filter( 'uap_option_all_wc_variable_products', 'your_function_name', 10, 1 );
Parameters
-
$option(mixed) - This parameter is used to specify whether to include variable products in the retrieved list.
Return Value
The filtered value.
Examples
<?php
/**
* Example: Add a custom token for WooCommerce variable products.
*
* This filter allows you to add additional relevant tokens to the options
* related to WooCommerce variable products within Uncanny Automator Pro.
*
* @param array $option The current option array, potentially containing 'relevant_tokens'.
* @return array The modified option array with added tokens.
*/
add_filter( 'uap_option_all_wc_variable_products', function( $option ) {
// Ensure 'relevant_tokens' key exists before trying to modify it.
if ( isset( $option['relevant_tokens'] ) && is_array( $option['relevant_tokens'] ) ) {
// Add a new custom token. For demonstration, let's say we want to include
// the product's SKU if it's a variable product.
// We'll assume $option['product_id'] is available and contains the product ID.
if ( isset( $option['product_id'] ) && $option['product_id'] > 0 ) {
$product = wc_get_product( $option['product_id'] );
if ( $product && $product->is_type( 'variable' ) ) {
$option['relevant_tokens']['VARIABLEPRODUCTSKU'] = sprintf(
// Translators: Placeholder for the variable product SKU.
esc_attr__( 'Variable product SKU: %s', 'your-text-domain' ),
$product->get_sku() ? $product->get_sku() : esc_attr__( 'N/A', 'your-text-domain' )
);
}
}
}
// Always return the $option array, whether modified or not.
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/wc-memberships/helpers/wc-memberships-helpers.php:77
uncanny-automator-pro/src/integrations/woocommerce/helpers/woocommerce-pro-helpers.php:735
public function wcm_get_all_membership_plans( $label = null, $option_code = 'WCMEMBERSHIPPLANS', $args = array() ) {
if ( ! $label ) {
$label = esc_html__( 'Membership plan', 'uncanny-automator' );
}
$token = key_exists( 'token', $args ) ? $args['token'] : false;
$is_ajax = key_exists( 'is_ajax', $args ) ? $args['is_ajax'] : false;
$include_all = key_exists( 'include_all', $args ) ? $args['include_all'] : '';
$is_any = key_exists( 'is_any', $args ) ? $args['is_any'] : false;
$end_point = key_exists( 'endpoint', $args ) ? $args['endpoint'] : '';
$relevant = key_exists( 'include_relevant_tokens', $args ) ? $args['include_relevant_tokens'] : false;
$args = array(
'post_type' => 'wc_membership_plan',
'posts_per_page' => 999,
'orderby' => 'title',
'order' => 'ASC',
'post_status' => 'publish',
);
$options = Automator()->helpers->recipe->options->wp_query( $args, $is_any, esc_attr__( 'Any membership plan', 'uncanny-automator' ) );
$option = array(
'option_code' => $option_code,
'label' => $label,
'input_type' => 'select',
'required' => true,
'supports_tokens' => $token,
'is_ajax' => $is_ajax,
'endpoint' => $end_point,
'options' => $options,
);
if ( $relevant ) {
$option['relevant_tokens'] = array(
$option_code => $label,
'WCMMEMBERSHIPPLANPOSTID' => esc_attr__( 'Membership plan post ID', 'uncanny-automator' ),
'WCMMEMBERSHIPPOSTID' => esc_attr__( 'Membership post ID', 'uncanny-automator' ),
);
}
return apply_filters( 'uap_option_all_wc_variable_products', $option );
}