Skip to content

Instantly share code, notes, and snippets.

@goranefbl
Created March 18, 2026 14:47
Show Gist options
  • Select an option

  • Save goranefbl/84d060867111fc03e6c78ec1f4ce5559 to your computer and use it in GitHub Desktop.

Select an option

Save goranefbl/84d060867111fc03e6c78ec1f4ce5559 to your computer and use it in GitHub Desktop.
snippet to run on past orders
<?php
/**
* Recalculate points for orders between Jan-Dec 2025 after BGN→EUR currency change.
* Run once via snippets plugin, then disable/delete.
*
* IMPORTANT: First adjust order totals from BGN to EUR, then run this snippet.
*/
add_action('init', function () {
// Safety: only run for admins
if (!current_user_can('manage_options')) return;
// Run-once flag
if (get_option('wpgens_bgn_eur_points_recalc_done')) return;
// Get earning action settings
$earning_actions = get_option('wpgens_loyalty_points_earning_actions', []);
$order_action = null;
foreach ($earning_actions as $action) {
if ($action['type'] === 'PLACE_ORDER' && $action['enabled']) {
$order_action = $action;
break;
}
}
if (!$order_action) {
error_log('WPGens Points Recalc: No enabled PLACE_ORDER earning action found.');
return;
}
$points_rate = floatval($order_action['points']);
$order_value = isset($order_action['orderValue']) ? floatval($order_action['orderValue']) : 1;
if ($order_value <= 0) $order_value = 1;
// Query orders from 2025
$orders = wc_get_orders([
'date_created' => '2025-01-01...2025-12-31',
'status' => ['completed', 'processing'],
'limit' => -1,
'meta_key' => '_wpgens_loyalty_points_awarded',
'meta_value' => '1',
]);
$log = [];
foreach ($orders as $order) {
$order_id = $order->get_id();
$user_id = $order->get_user_id();
// Skip guest orders with no user
if (!$user_id) continue;
$old_points = intval($order->get_meta('_wpgens_loyalty_points_amount'));
// Recalculate points based on current order total
$order_total = floatval($order->get_total());
$order_total_without_shipping = $order_total
- floatval($order->get_shipping_total())
- floatval($order->get_shipping_tax());
if ($order_value > 1) {
$new_points = (int) floor($order_total_without_shipping * $points_rate / $order_value);
} else {
$new_points = (int) floor($order_total_without_shipping * $points_rate);
}
$difference = $new_points - $old_points;
if ($difference === 0) continue;
// Adjust user balance
do_action(
'wpgens_loyalty_update_points',
$user_id,
$difference,
$difference > 0 ? 'EARN' : 'DEDUCT',
'ADMIN_ADJUSTMENT',
$order_id
);
// Update order meta
$order->update_meta_data('_wpgens_loyalty_points_amount', $new_points);
$order->save();
$log[] = sprintf(
'Order #%d (user %d): %d → %d points (diff: %+d, total: %s)',
$order_id, $user_id, $old_points, $new_points, $difference, $order_total
);
}
// Mark as done
update_option('wpgens_bgn_eur_points_recalc_done', current_time('mysql'));
// Log results
error_log('WPGens Points Recalc completed. ' . count($log) . ' orders adjusted.');
foreach ($log as $entry) {
error_log(' ' . $entry);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment