Skip to content

Instantly share code, notes, and snippets.

@goranefbl
Created January 7, 2026 21:35
Show Gist options
  • Select an option

  • Save goranefbl/0808292d5b5ac98daa0134eaf3cbd8ec to your computer and use it in GitHub Desktop.

Select an option

Save goranefbl/0808292d5b5ac98daa0134eaf3cbd8ec to your computer and use it in GitHub Desktop.
Limit maximum number of points discount
<?php
/**
* Limit TOTAL discount (coupons + points) to a maximum percentage
*
* This ensures customers can't stack coupon codes with points
* to get more than a specified total discount.
*/
add_filter('wpgens_loyalty_calculate_points_discount', function($discount_amount, $points, $conversion_rate) {
if (!function_exists('WC') || !WC()->cart) {
return $discount_amount;
}
$cart = WC()->cart;
// Maximum total discount allowed (points + coupons) as percentage
$max_total_discount_percent = 25;
// Get cart subtotal (before discounts)
$cart_subtotal = $cart->get_subtotal() + $cart->get_subtotal_tax();
if ($cart_subtotal <= 0) {
return $discount_amount;
}
// Calculate maximum total discount allowed
$max_total_discount = ($cart_subtotal * $max_total_discount_percent) / 100;
// Get current coupon discounts (excludes points coupon)
$coupon_discount = 0;
foreach ($cart->get_applied_coupons() as $coupon_code) {
// Skip loyalty points coupons
if (strpos($coupon_code, 'loyalty_points_') === 0) {
continue;
}
$coupon_discount += $cart->get_coupon_discount_amount($coupon_code, $cart->display_cart_ex_tax);
}
// Calculate remaining discount allowance for points
$remaining_allowance = $max_total_discount - $coupon_discount;
// If coupons already exceed the limit, no points discount allowed
if ($remaining_allowance <= 0) {
return 0;
}
// Cap points discount to remaining allowance
return min($discount_amount, $remaining_allowance);
}, 20, 3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment