<?php if (!defined('ABSPATH')) exit; class GT_BOGO_Streak_Bonus { public static function init() { add_action('woocommerce_thankyou', array(__CLASS__, 'update_streak'), 10, 1); add_action('woocommerce_account_dashboard', array(__CLASS__, 'display_streak_calendar')); } public static function update_streak($order_id) { $order = wc_get_order($order_id); if (!$order) return; $user_id = $order->get_customer_id(); if (!$user_id) return; $current_streak = (int) get_user_meta($user_id, 'gtbogo_current_streak', true); $last_purchase = get_user_meta($user_id, 'gtbogo_last_purchase_week', true); $this_week = date('Y-W'); if ($last_purchase === $this_week) { return; } $last_week_num = $last_purchase ? date('W', strtotime($last_purchase)) : 0; $this_week_num = date('W'); if ($this_week_num == $last_week_num + 1) { $current_streak++; update_user_meta($user_id, 'gtbogo_current_streak', $current_streak); if ($current_streak == 3) { self::grant_streak_reward($user_id, 'bronze', 5); } elseif ($current_streak == 6) { self::grant_streak_reward($user_id, 'silver', 10); } elseif ($current_streak == 12) { self::grant_streak_reward($user_id, 'gold', 20); } } else { $current_streak = 1; update_user_meta($user_id, 'gtbogo_current_streak', $current_streak); } update_user_meta($user_id, 'gtbogo_last_purchase_week', $this_week); update_user_meta($user_id, 'gtbogo_best_streak', max($current_streak, (int) get_user_meta($user_id, 'gtbogo_best_streak', true))); } public static function display_streak_calendar() { $user_id = get_current_user_id(); if (!$user_id) return; $current_streak = (int) get_user_meta($user_id, 'gtbogo_current_streak', true); $best_streak = (int) get_user_meta($user_id, 'gtbogo_best_streak', true); echo '<div class="gtbogo-streak-card" style="background: linear-gradient(135deg, #ff0000 0%, #cc0000 100%); padding: 30px; border-radius: 12px; margin: 20px 0; color: #fff;">'; echo '<h3 style="margin: 0 0 20px 0; font-size: 1.1em; font-weight: 600; letter-spacing: 0.05em;">Shopping Streak</h3>'; echo '<div style="display: flex; gap: 40px; align-items: center;">'; echo '<div>'; echo '<div style="font-size: 48px; font-weight: 700;">' . $current_streak . '</div>'; echo '<div style="opacity: 0.9;">Week' . ($current_streak == 1 ? '' : 's') . ' in a row</div>'; echo '</div>'; echo '<div style="flex: 1;">'; echo '<p style="margin: 0 0 10px 0;"><strong>Best Streak:</strong> ' . $best_streak . ' weeks</p>'; echo '<p style="margin: 0; opacity: 0.9; font-size: 14px;">Shop every week to unlock exclusive BOGO rewards!</p>'; echo '</div>'; echo '</div>'; $next_milestone = 3; if ($current_streak >= 3) $next_milestone = 6; if ($current_streak >= 6) $next_milestone = 12; if ($current_streak < 12) { echo '<div style="margin-top: 20px; padding-top: 20px; border-top: 1px solid rgba(255,255,255,0.2);">'; echo '<p style="margin: 0; font-size: 14px; opacity: 0.9;">Next Reward: ' . ($next_milestone - $current_streak) . ' more week' . (($next_milestone - $current_streak) == 1 ? '' : 's') . ' to unlock '; echo ($next_milestone == 3 ? '5% bonus' : ($next_milestone == 6 ? '10% bonus' : '20% VIP bonus')) . '!</p>'; echo '</div>'; } echo '</div>'; } private static function grant_streak_reward($user_id, $level, $bonus) { $user = get_user_by('ID', $user_id); if (!$user) return; $message = '<h2>' . $bonus . '% loyalty bonus applied</h2>'; $message .= '<p>You\'ve shopped for ' . ($level == 'bronze' ? '3' : ($level == 'silver' ? '6' : '12')) . ' consecutive weeks!</p>'; $message .= '<p>Enjoy your ' . $bonus . '% extra BOGO discount on your next purchase.</p>'; wp_mail( $user->user_email, 'Your loyalty bonus has been applied', $message, array('Content-Type: text/html; charset=UTF-8') ); } } GT_BOGO_Streak_Bonus::init();