ulgm_reduce_qty_on_refund
Filters the quantity reduction applied when a refund is processed for a grouped product.
add_filter( 'ulgm_reduce_qty_on_refund', $callback, 10, 2 );
Description
This filter determines if a license's quantity should be reduced upon a WooCommerce refund. Developers can use this to conditionally prevent or modify license quantity reduction based on order details or other logic. The original and real order IDs are available for context.
Usage
add_filter( 'ulgm_reduce_qty_on_refund', 'your_function_name', 10, 2 );
Parameters
-
$this(mixed) - This parameter represents the current instance of the `woocommerce-license-refund` class.
-
$group_id(mixed) - This parameter represents the current instance of the `WooCommerce_License_Refund` class, providing access to its methods and properties.
Return Value
The filtered value.
Examples
/**
* Filter hook to determine which license items should have their quantities reduced on refund.
*
* This function intercepts the default behavior of reducing quantities on license refunds
* and allows for more granular control. It checks if the order is linked to a group,
* if the product is a license, and if the specific license product is configured to
* reduce quantities on refund.
*
* @param array $license_items An array of line items that are identified as licenses.
* @param int $group_id The ID of the license group associated with the order.
* @return array An array of license items that should have their quantities reduced.
*/
add_filter( 'ulgm_reduce_qty_on_refund', function( $license_items, $group_id ) {
// If the group ID is empty, we can't proceed with group-specific logic.
if ( empty( $group_id ) ) {
return $license_items;
}
// Assume we have a way to get license product IDs associated with a group.
// Replace this with actual logic to fetch relevant license product IDs for the group.
$group_license_product_ids = get_option( 'ulgm_group_license_products_' . $group_id, array() );
// If no specific license products are configured for this group to reduce quantities,
// return all identified license items for further processing by WooCommerce or other plugins.
if ( empty( $group_license_product_ids ) ) {
return $license_items;
}
$items_to_reduce = array();
foreach ( $license_items as $line_item_id => $line_item_details ) {
$product_id = $line_item_details['product_id'];
// Check if the product ID of the current license item is in the list
// of license products configured for this group to reduce quantities.
if ( in_array( $product_id, $group_license_product_ids, true ) ) {
$items_to_reduce[ $line_item_id ] = $line_item_details;
}
}
// Return only the license items that are configured to reduce quantities for this group.
return $items_to_reduce;
}, 10, 2 ); // Priority 10, accepts 2 arguments
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-refund.php:216
public function is_refunding_a_license( $args ) {
if ( true !== $this->reduce_the_qty_on_refund() ) {
return array();
}
$original_id = $args['order_id'];
$order_id = $this->get_real_order_id( $original_id );
if ( ! $order_id ) {
return array();
}
$this->order_id = $order_id;
// Check if there's a valid order ID
$order = wc_get_order( $original_id );
$this->order = $order;
if ( ! $order instanceof WC_Order ) {
return array();
}
// Check if there's a group linked to the order
$group_id = $order->get_meta( SharedFunctions::$linked_group_id_meta, true );
if ( empty( $group_id ) ) {
return array();
}
$this->group_id = $group_id;
// Allow users to skip some groups
if ( true !== apply_filters( 'ulgm_reduce_qty_on_refund', $this->reduce_the_qty_on_refund(), $group_id ) ) {
return array();
}
$line_items = $args['line_items'];
if ( empty( $line_items ) ) {
return array();
}
$license_items = array();
foreach ( $line_items as $line_item_id => $line_item_details ) {
$product_id = $this->get_product_id( $line_item_id );
if ( ! $this->is_a_license( $product_id ) ) {
continue;
}
$line_item_details['product_id'] = $product_id;
$license_items[ $line_item_id ] = $line_item_details;
}
return $license_items;
}