Last active
March 1, 2026 06:42
-
-
Save goranefbl/f9811a5f63c3d89790475b667f99c224 to your computer and use it in GitHub Desktop.
Auto apply rank rewards
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 | |
| // 1. Auto 10% discount for users in a specific rank (e.g. Silver or higher) | |
| add_action('woocommerce_cart_calculate_fees', function ($cart) { | |
| if (is_admin() && !defined('DOING_AJAX')) return; | |
| $user_id = get_current_user_id(); | |
| if (!$user_id || !class_exists('WPGL_Ranks_Core')) return; | |
| $rank_slug_or_id = 'silver'; // slug or rank ID; true = this rank or higher | |
| if (!WPGL_Ranks_Core::check_user_rank($user_id, $rank_slug_or_id, true)) return; | |
| $user_rank = WPGL_Ranks_Core::get_user_rank($user_id); | |
| $subtotal = (float) $cart->get_subtotal(); | |
| if ($subtotal <= 0) return; | |
| $percent = 10; | |
| $discount_amount = -1 * round($subtotal * ($percent / 100), wc_get_price_decimals()); | |
| $rank_name = $user_rank && isset($user_rank->name) ? $user_rank->name : 'Member'; | |
| $cart->add_fee(sprintf('%1$s discount (%2$d%%)', $rank_name, $percent), $discount_amount, true); | |
| }); | |
| // 2. Auto-apply rank reward coupons (discount/shipping from rank rewards) | |
| add_action('woocommerce_before_cart', 'wpgl_auto_apply_rank_reward_coupons'); | |
| add_action('woocommerce_before_checkout_form', 'wpgl_auto_apply_rank_reward_coupons'); | |
| add_action('woocommerce_add_to_cart', 'wpgl_auto_apply_rank_reward_coupons'); | |
| add_action('woocommerce_cart_loaded_from_session', 'wpgl_auto_apply_rank_reward_coupons'); | |
| function wpgl_auto_apply_rank_reward_coupons() { | |
| if (!function_exists('WC') || !WC()->cart || WC()->cart->cart_contents_count < 1) return; | |
| $user_id = get_current_user_id(); | |
| if (!$user_id || !class_exists('WPGL_Ranks_Database')) return; | |
| $claimed = WPGL_Ranks_Database::get_user_claimed_rewards($user_id); | |
| if (empty($claimed)) return; | |
| $coupon_codes = []; | |
| foreach ($claimed as $row) { | |
| $data = isset($row->reward_data) ? json_decode($row->reward_data, true) : []; | |
| if (!is_array($data)) continue; | |
| $type = isset($data['type']) ? $data['type'] : ''; | |
| if ($type !== 'discount' && $type !== 'shipping') continue; | |
| if (!empty($data['coupon_code'])) $coupon_codes[] = $data['coupon_code']; | |
| } | |
| if (empty($coupon_codes)) return; | |
| $applied = WC()->cart->get_applied_coupons(); | |
| foreach ($coupon_codes as $code) { | |
| if (in_array($code, $applied, true)) return; | |
| $coupon = new WC_Coupon($code); | |
| if ($coupon->get_id() && wpgl_is_coupon_valid_for_cart($coupon)) { | |
| WC()->cart->add_discount($code); | |
| return; | |
| } | |
| } | |
| } | |
| function wpgl_is_coupon_valid_for_cart($coupon) { | |
| if (!$coupon->get_id()) return false; | |
| if ($coupon->get_date_expires() && $coupon->get_date_expires()->getTimestamp() < time()) return false; | |
| $user_id = get_current_user_id(); | |
| if ($user_id) { | |
| $limit = $coupon->get_usage_limit(); | |
| if ($limit > 0 && $coupon->get_usage_count() >= $limit) return false; | |
| $limit_per_user = $coupon->get_usage_limit_per_user(); | |
| if ($limit_per_user !== null && $limit_per_user > 0) { | |
| $used_by = $coupon->get_used_by(); | |
| $n = 0; | |
| foreach ($used_by as $uid) { if ((int) $uid === $user_id) $n++; } | |
| if ($n >= $limit_per_user) return false; | |
| } | |
| $emails = $coupon->get_email_restrictions(); | |
| if (!empty($emails)) { | |
| $user = get_userdata($user_id); | |
| if ($user && !in_array($user->user_email, $emails, true)) return false; | |
| } | |
| } | |
| return true; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment