ulgm_swap_product_types
Filters the product types used by Ultimate LGA Manager, allowing modification of available types before they are displayed or processed.
add_filter( 'ulgm_swap_product_types', $callback, 10, 2 );
Description
Allows developers to filter the product types available when swapping licenses. Modify the `$types` array to include or exclude specific product types, offering custom control over which products can be associated with license swaps. This filter is applied before fetching product types for the swap interface.
Usage
add_filter( 'ulgm_swap_product_types', 'your_function_name', 10, 2 );
Parameters
-
$types(mixed) - This parameter holds an array of available product types that can be swapped.
-
$types(mixed) - This parameter contains an array of available WooCommerce product types.
Return Value
The filtered value.
Examples
/**
* Example of how to filter the product types available for the license swap.
* This example will only allow Simple and External products to be selected.
*
* @param array $types An array of product type slugs.
* @param array $all_types An array of all available product types.
* @return array The filtered array of product types.
*/
add_filter(
'ulgm_swap_product_types',
function( $types, $all_types ) {
// Define the product types we want to allow.
$allowed_types = array( 'simple', 'external' );
// Filter the $types array to only include allowed types.
$filtered_types = array_intersect( $types, $allowed_types );
// If there are no allowed types found, ensure we return an empty array
// to prevent potential errors when wc_get_products is called later.
if ( empty( $filtered_types ) ) {
return array();
}
return $filtered_types;
},
10, // Priority
2 // Accepted args count
);
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/classes/woocommerce/woocommerce-group-license-swap.php:152
public function backend_display_swap_product_field() {
global $woocommerce, $post;
$types = wc_get_product_types();
// exclude variable products
if ( isset( $types['variable'] ) ) {
unset( $types['variable'] );
}
$args = array(
'type' => apply_filters( 'ulgm_swap_product_types', array_keys( $types ), $types ),
'status' => array( 'publish' ),
'limit' => '-1',
);
$current_value = 0;
// exclude current product
if ( is_object( $post ) && $post->ID ) {
$args['exclude'] = array( $post->ID );
$current_value = get_post_meta( $post->ID, 'ulgm_swap_license_product', true );
}
$raw_products = wc_get_products( $args );
$products = array();
$products['0'] = __( 'Select a product', 'uncanny-learndash-groups' );
if ( ! empty( $raw_products ) ) {
foreach ( $raw_products as $raw_product ) {
$products[ $raw_product->get_id() ] = $raw_product->get_name() . ' (#' . $raw_product->get_id() . ')';
}
}
echo '<div id="ulgm_swap_license_product_wrapper" style="display: none;">';
woocommerce_wp_select(
array(
'id' => 'ulgm_swap_license_product',
'class' => 'select long select2',
'label' => __( 'Swap course product ', 'uncanny-learndash-groups' ),
'selected' => true,
'value' => $current_value,
'options' => apply_filters( 'ulgm_swap_products', $products, $post ),
'desc_tip' => true,
'style' => 'width: 50%"',
'description' => __( 'If a quantity of 1 for this product is added to the cart, swap it for the selected product instead', 'uncanny-learndash-groups' ),
)
);
echo '</div>';
}