Created
January 5, 2026 15:44
-
-
Save goranefbl/a959b40785a2d4b7d07915aa8e334caa to your computer and use it in GitHub Desktop.
recalculate order points
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
| /** | |
| * Recalculate loyalty points when an order is updated in admin | |
| */ | |
| add_action('woocommerce_process_shop_order_meta', 'recalculate_loyalty_points_on_order_update', 50, 1); | |
| function recalculate_loyalty_points_on_order_update($order_id) { | |
| $order = wc_get_order($order_id); | |
| if (!$order) { | |
| return; | |
| } | |
| // Only recalculate if points haven't been awarded yet | |
| // (once awarded, changing order shouldn't change user's points) | |
| if ($order->get_meta('_wpgens_loyalty_points_awarded')) { | |
| return; | |
| } | |
| // Clear existing calculated points to allow recalculation | |
| $order->delete_meta_data('_wpgens_loyalty_points_amount'); | |
| $order->delete_meta_data('_wpgens_loyalty_points_rate'); | |
| // Also clear item-level points meta | |
| foreach ($order->get_items() as $item) { | |
| $item->delete_meta_data('_wpgens_loyalty_points_earned'); | |
| $item->delete_meta_data('_wpgens_loyalty_points_rate'); | |
| $item->save(); | |
| } | |
| $order->save(); | |
| // Now trigger recalculation | |
| if (class_exists('WPGL_Points_Checkout')) { | |
| WPGL_Points_Checkout::calculate_order_points($order_id); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment