Created
February 24, 2026 15:21
-
-
Save goranefbl/2aeb48e19981e39a761150efc04c6738 to your computer and use it in GitHub Desktop.
Lifetime discounts - loyalty plugin by wpgens for woocommerce
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 | |
| /** | |
| * Diamond Rank — Automatic 15% lifetime discount, no points. | |
| * | |
| * 1. Applies a 15% cart discount on every order for Diamond rank users. | |
| * 2. Disables points earning/redeeming for Diamond rank users (they get the | |
| * flat discount instead). | |
| * | |
| * Requirements: | |
| * - A rank with slug "diamond" must exist in the Ranks module. | |
| * - Adjust the slug in DIAMOND_RANK_SLUG if your rank uses a different slug. | |
| */ | |
| // ── Config ────────────────────────────────────────────────────── | |
| define('WPGL_DIAMOND_RANK_SLUG', 'diamond'); // Change if your rank slug differs | |
| define('WPGL_DIAMOND_DISCOUNT_PCT', 15); // Discount percentage | |
| // ── 1. Auto-apply 15% discount at cart/checkout ───────────────── | |
| add_action('woocommerce_cart_calculate_fees', function () { | |
| if (is_admin() && !defined('DOING_AJAX')) { | |
| return; | |
| } | |
| $user_id = get_current_user_id(); | |
| if (!$user_id) { | |
| return; | |
| } | |
| // Make sure the Ranks module is loaded | |
| if (!class_exists('WPGL_Ranks_Core') || !WPGL_Ranks_Core::is_active()) { | |
| return; | |
| } | |
| // Check if the user has Diamond rank (exact match, not "or higher") | |
| if (!WPGL_Ranks_Core::check_user_rank($user_id, WPGL_DIAMOND_RANK_SLUG, false)) { | |
| return; | |
| } | |
| $cart = WC()->cart; | |
| if (!$cart) { | |
| return; | |
| } | |
| // Calculate discount on cart subtotal (excl. tax to avoid double-taxing the discount) | |
| $subtotal = (float) $cart->get_subtotal(); | |
| if ($subtotal <= 0) { | |
| return; | |
| } | |
| $discount = round($subtotal * (WPGL_DIAMOND_DISCOUNT_PCT / 100), 2); | |
| // Add as a negative fee (= discount). WooCommerce will handle tax on the discounted total. | |
| $cart->add_fee( | |
| sprintf(__('Diamond Member Discount (%d%%)', 'wpgens-loyalty'), WPGL_DIAMOND_DISCOUNT_PCT), | |
| -$discount | |
| ); | |
| }); | |
| // ── 2. Disable points earning & redeeming for Diamond members ─── | |
| add_filter('wpgens_loyalty_user_can_participate', function ($is_allowed, $user_id) { | |
| if (!$is_allowed) { | |
| return false; | |
| } | |
| if (!class_exists('WPGL_Ranks_Core') || !WPGL_Ranks_Core::is_active()) { | |
| return $is_allowed; | |
| } | |
| // Diamond members don't use the points system — their benefit is the flat discount | |
| if (WPGL_Ranks_Core::check_user_rank($user_id, WPGL_DIAMOND_RANK_SLUG, false)) { | |
| return false; | |
| } | |
| return $is_allowed; | |
| }, 20, 2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment