ulgm_swap_products
Filters product data before it is displayed, allowing modification of the product array based on the current post.
add_filter( 'ulgm_swap_products', $callback, 10, 2 );
Description
Allows developers to modify the array of available products when a license is being swapped. Modify the `$products` array to filter which products are displayed as swap options, or alter the `$post` object for context-specific filtering.
Usage
add_filter( 'ulgm_swap_products', 'your_function_name', 10, 2 );
Parameters
-
$products(mixed) - This parameter contains an array of WooCommerce product types that are eligible for swapping.
-
$post(mixed) - This parameter contains the products that will be displayed in the swap product dropdown.
Return Value
The filtered value.
Examples
add_filter( 'ulgm_swap_products', 'my_custom_swap_products_filter', 10, 2 );
/**
* Custom filter example for ulgm_swap_products.
*
* This example demonstrates how to filter the list of products available for swapping.
* It adds a specific product to the list if it's not already present and removes
* any products with a price less than a certain threshold.
*
* @param array $products The array of products to be displayed in the swap dropdown.
* @param WP_Post $post The current post object.
* @return array The modified array of products.
*/
function my_custom_swap_products_filter( $products, $post ) {
// Add a specific product if it's not already in the list.
// Replace '123' with the actual product ID you want to ensure is available.
$product_id_to_add = 123;
if ( ! isset( $products[ $product_id_to_add ] ) ) {
$product_to_add = wc_get_product( $product_id_to_add );
if ( $product_to_add ) {
$products[ $product_id_to_add ] = $product_to_add->get_name() . ' (#' . $product_id_to_add . ')';
}
}
// Remove products with a price less than 50.
// This is just an example; adjust the threshold as needed.
$min_price_threshold = 50;
foreach ( $products as $product_id => $product_name ) {
// Skip the default "Select a product" option.
if ( $product_id === '0' ) {
continue;
}
$product = wc_get_product( $product_id );
if ( $product && $product->get_price() < $min_price_threshold ) {
unset( $products[ $product_id ] );
}
}
// Ensure the array keys are re-indexed if items were removed.
return array_values( $products );
}
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:183
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>';
}