ulgm_is_swappable_product
Filters the product ID to determine if a license product is swappable for a different product.
add_filter( 'ulgm_is_swappable_product', $callback, 10, 1 );
Description
Filters whether a product is eligible for license swapping. Developers can modify the `$swap_license_product_id` to control which product is associated with the swap, effectively enabling or disabling swapping for specific products. This hook fires after initial product type checks but before the swap product is fetched.
Usage
add_filter( 'ulgm_is_swappable_product', 'your_function_name', 10, 1 );
Parameters
-
$swap_license_product_id(mixed) - This parameter contains the ID of the product that the current product can be swapped with.
Return Value
The filtered value.
Examples
add_filter( 'ulgm_is_swappable_product', 'my_custom_swappable_product_logic', 10, 1 );
/**
* Custom logic to determine if a product is swappable based on specific criteria.
*
* @param int $swap_license_product_id The ID of the product to swap to, if set.
* @return int The product ID to swap to, or 0 if not swappable.
*/
function my_custom_swappable_product_logic( $swap_license_product_id ) {
// For demonstration, let's say we want to disable swapping for any product
// that has a specific tag, e.g., 'no-swap'.
$product_id = get_the_ID(); // Assuming this filter is called within the WordPress loop
if ( ! $product_id ) {
return $swap_license_product_id; // Return original if we can't get product ID
}
$product = wc_get_product( $product_id );
if ( ! $product ) {
return $swap_license_product_id;
}
// Check if the product has the 'no-swap' tag
if ( has_term( 'no-swap', 'product_tag', $product_id ) ) {
// If it has the 'no-swap' tag, override the swap product ID to 0,
// effectively making it not swappable.
return 0;
}
// If the product doesn't have the 'no-swap' tag, return the original
// swap product ID as determined by the plugin.
return $swap_license_product_id;
}
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:103
private function is_swappable( $product_id ) {
$product = wc_get_product( $product_id );
if ( ! $product ) {
return 0;
}
if ( ! in_array( $product->get_type(), array( 'license', 'subscription' ), true ) ) {
return 0;
}
$swap_license_product_id = absint( get_post_meta( $product_id, 'ulgm_swap_license_product', true ) );
if ( $swap_license_product_id > 0 ) {
$swap_adding_to_cart = wc_get_product( $swap_license_product_id );
if ( ! $swap_adding_to_cart ) {
return 0;
}
}
return apply_filters( 'ulgm_is_swappable_product', $swap_license_product_id );
}