Created
July 30, 2025 20:28
-
-
Save goranefbl/361b88fcd474657a9d688eb06f9c6d0d to your computer and use it in GitHub Desktop.
prevent coupons when points applied.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Add this to your main plugin file or in a hooks class | |
add_filter('woocommerce_coupon_is_valid', 'prevent_coupons_when_points_applied', 10, 3); | |
function prevent_coupons_when_points_applied($is_valid, $coupon, $discount_object) { | |
// Skip validation for loyalty points coupons themselves | |
if (strpos($coupon->get_code(), 'loyalty_points_') === 0) { | |
return $is_valid; | |
} | |
// Check if any points coupon is currently applied | |
if (function_exists('WC') && WC()->cart) { | |
$applied_coupons = WC()->cart->get_applied_coupons(); | |
foreach ($applied_coupons as $applied_coupon_code) { | |
if (strpos($applied_coupon_code, 'loyalty_points_') === 0) { | |
// Verify the points coupon is still valid via transient | |
$coupon_data = get_transient('loyalty_coupon_' . $applied_coupon_code); | |
if ($coupon_data && $coupon_data['user_id'] === get_current_user_id()) { | |
throw new Exception(__('Coupons cannot be used when loyalty points are applied. Please remove points discount first.', 'wpgens-points-and-rewards-program')); | |
} | |
} | |
} | |
} | |
return $is_valid; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment