ulgm_per_seat_text_with_qty
Filters the per-seat text, allowing customization of how quantity is displayed for each seat.
add_filter( 'ulgm_per_seat_text_with_qty', $callback, 10, 3 );
Description
Filters the text displayed for per-seat licensing in WooCommerce cart items, including quantity. Developers can modify the quantity and the "per seat" text to customize how license quantities are presented to users. This hook fires after license product checks but before the text is output.
Usage
add_filter( 'ulgm_per_seat_text_with_qty', 'your_function_name', 10, 3 );
Parameters
-
$qty(mixed) - This parameter represents the quantity of the license item in the cart.
-
$qty(mixed) - This parameter is not used in the provided code snippet and its purpose is unclear from the context.
-
$per_seat_text(mixed) - This parameter holds the current cart item data, which is used to determine the product and its properties.
Return Value
The filtered value.
Examples
// Filter the "per seat" text to include the product name for clarity.
add_filter( 'ulgm_per_seat_text_with_qty', 'my_plugin_add_product_to_per_seat_text', 10, 3 );
function my_plugin_add_product_to_per_seat_text( $per_seat_text_with_qty, $qty, $per_seat_text ) {
// We need access to the cart item to get the product object.
// This filter hook doesn't directly pass the cart item, but the source
// context shows it's part of a method that has access to it.
// For this example, we'll assume we can retrieve it via a global
// or by re-hooking into a more suitable WordPress action if needed.
// In a real-world scenario, you'd likely have access to $cart_item
// within the same context or through another mechanism.
// For demonstration, let's simulate getting the product object.
// In a real plugin, this would be more robust.
global $woocommerce;
$cart_items = $woocommerce->cart->get_cart();
$current_product = null;
// Find the product associated with this cart item (this is a simplified approach)
foreach ( $cart_items as $cart_item_key => $cart_item ) {
// We are trying to match to the current context, which might be difficult
// without the $cart_item_key being directly passed or accessible.
// Assuming for this example, we can infer it.
// A better approach would be if the original function passed the $cart_item_key.
// Let's assume for the sake of example that $qty and $per_seat_text
// are enough to identify the context, or that the calling function
// has a way to relate this back.
// A more realistic approach if the $cart_item_key was available:
// if ( $cart_item_key === $original_cart_item_key_from_context ) {
// $current_product = $cart_item['data'];
// break;
// }
// As a placeholder, let's just grab the first license product we find
// if we can't reliably link it. This is NOT production-ready.
if ( isset( $cart_item['data'] ) && $cart_item['data'] instanceof WC_Product && $cart_item['data']->is_type( 'license' ) ) {
$current_product = $cart_item['data'];
break; // Found a license product, use it.
}
}
if ( $current_product ) {
$product_name = $current_product->get_name();
// Append the product name to the "per seat" text.
$modified_per_seat_text_with_qty = sprintf(
'%s (%s)',
$per_seat_text_with_qty,
$product_name
);
return $modified_per_seat_text_with_qty;
}
// If we couldn't get product info, return the original text.
return $per_seat_text_with_qty;
}
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-license.php:564
public function woocommerce_cart_item_qty_func( $qty, $cart_item, $cart_item_key ) {
if ( empty( $cart_item ) ) {
return $qty;
}
$product = $cart_item['data'];
if ( ! $product instanceof WC_Product || ! $product->is_type( 'license' ) ) {
return $qty;
}
if ( true === apply_filters( 'ulgm_show_per_seat_with_qty', true, $cart_item, $cart_item_key ) ) {
$per_seat_text = get_option( 'ulgm_per_seat_text_plural', 'Seats' );
$qty = apply_filters(
'ulgm_per_seat_text_with_qty',
sprintf(
'%s %s',
$qty,
$per_seat_text
),
$qty,
$per_seat_text
);
}
return $qty;
}