Created
January 26, 2026 02:47
-
-
Save TanvirHasan19/15d0df1b97bb34576628d20e79be45c7 to your computer and use it in GitHub Desktop.
Bulk reset store credits to zero for all users in a given role(s).
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
| /** | |
| * Bulk reset store credits to zero for all users in a given role(s). | |
| * | |
| * SETUP: | |
| * 1. Set the role(s) in ACFW_BULK_RESET_DEFAULT_ROLE below (one role, or comma-separated: 'wholesale_customer,trade'). | |
| * 2. Add this snippet (e.g. Code Snippets or a custom plugin). Advanced Coupons (Store Credits) must be active. | |
| * | |
| * TO RUN: | |
| * - Option A: Go to the Dashboard — a "Run now" link is shown. Click it (must have manage_woocommerce). | |
| * - Option B: Open the URL manually with ?role=ROLE and a valid _wpnonce (e.g. from the Dashboard link). The role in the URL overrides the default. | |
| */ | |
| // ------------- SET YOUR ROLE(S) HERE (comma-separated for multiple) ------------- | |
| define( 'ACFW_BULK_RESET_DEFAULT_ROLE', 'customer' ); | |
| // ------------------------------------------------------------------------------- | |
| add_action( 'admin_init', function() { | |
| if ( ! current_user_can( 'manage_woocommerce' ) ) { | |
| return; | |
| } | |
| if ( empty( $_GET['acfw_bulk_reset_sc'] ) || empty( $_GET['role'] ) ) { | |
| return; | |
| } | |
| if ( empty( $_GET['_wpnonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'acfw_bulk_reset_sc' ) ) { | |
| wp_die( esc_html__( 'Invalid or expired link. Please use the Dashboard link or create a new URL with a fresh nonce.', 'acfw-bulk-reset' ) ); | |
| } | |
| $role_input = sanitize_text_field( wp_unslash( $_GET['role'] ) ); | |
| $roles = array_filter( array_map( 'trim', explode( ',', $role_input ) ) ); | |
| if ( empty( $roles ) ) { | |
| wp_die( esc_html__( 'Please provide a valid role (or comma-separated roles).', 'acfw-bulk-reset' ) ); | |
| } | |
| if ( ! function_exists( 'ACFWF' ) || ! is_object( ACFWF()->Store_Credits_Calculate ) ) { | |
| wp_die( esc_html__( 'Advanced Coupons (Store Credits) must be active.', 'acfw-bulk-reset' ) ); | |
| } | |
| @set_time_limit( 300 ); | |
| $user_ids = get_users( array( 'role__in' => $roles, 'fields' => 'ID', 'number' => -1 ) ); | |
| $reset = 0; | |
| $skipped = 0; | |
| $errors = array(); | |
| foreach ( $user_ids as $user_id ) { | |
| $user_id = (int) $user_id; | |
| $balance = ACFWF()->Store_Credits_Calculate->get_customer_balance( $user_id, true ); | |
| if ( $balance <= 0 ) { | |
| $skipped++; | |
| continue; | |
| } | |
| $entry = new \ACFWF\Models\Objects\Store_Credit_Entry(); | |
| $entry->set_prop( 'amount', $balance ); | |
| $entry->set_prop( 'user_id', $user_id ); | |
| $entry->set_prop( 'type', 'decrease' ); | |
| $entry->set_prop( 'action', 'admin_decrease' ); | |
| $entry->set_prop( 'object_id', get_current_user_id() ?: 1 ); | |
| $entry->set_prop( 'note', sprintf( 'Bulk reset for role(s): %s', $role_input ) ); | |
| $check = $entry->save( true ); | |
| if ( is_wp_error( $check ) ) { | |
| $errors[] = 'User ' . $user_id . ': ' . $check->get_error_message(); | |
| continue; | |
| } | |
| ACFWF()->Store_Credits_Calculate->get_customer_balance( $user_id, true ); | |
| $reset++; | |
| } | |
| set_transient( 'acfw_bulk_reset_message', array( 'reset' => $reset, 'skipped' => $skipped, 'errors' => $errors ), 60 ); | |
| wp_safe_redirect( add_query_arg( 'acfw_bulk_done', '1', admin_url( 'index.php' ) ) ); | |
| exit; | |
| }, 5 ); | |
| add_action( 'admin_notices', function() { | |
| if ( ! current_user_can( 'manage_woocommerce' ) ) { | |
| return; | |
| } | |
| // Success message after redirect | |
| if ( ! empty( $_GET['acfw_bulk_done'] ) ) { | |
| $data = get_transient( 'acfw_bulk_reset_message' ); | |
| if ( $data ) { | |
| delete_transient( 'acfw_bulk_reset_message' ); | |
| $msg = sprintf( | |
| __( 'Store credits bulk reset: %1$d user(s) set to zero, %2$d skipped (already zero).', 'acfw-bulk-reset' ), | |
| $data['reset'], | |
| $data['skipped'] | |
| ); | |
| if ( ! empty( $data['errors'] ) ) { | |
| $msg .= ' ' . __( 'Errors:', 'acfw-bulk-reset' ) . ' ' . implode( '; ', array_slice( $data['errors'], 0, 5 ) ); | |
| if ( count( $data['errors'] ) > 5 ) { | |
| $msg .= ' …'; | |
| } | |
| } | |
| echo '<div class="notice notice-success is-dismissible"><p>' . esc_html( $msg ) . '</p></div>'; | |
| return; | |
| } | |
| } | |
| // Dashboard: "Run bulk reset" link (uses ACFW_BULK_RESET_DEFAULT_ROLE) | |
| if ( ! defined( 'ACFW_BULK_RESET_DEFAULT_ROLE' ) || ACFW_BULK_RESET_DEFAULT_ROLE === '' ) { | |
| return; | |
| } | |
| $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; | |
| if ( ! $screen || $screen->id !== 'dashboard' ) { | |
| return; | |
| } | |
| $url = wp_nonce_url( | |
| add_query_arg( array( 'acfw_bulk_reset_sc' => '1', 'role' => ACFW_BULK_RESET_DEFAULT_ROLE ), admin_url( 'index.php' ) ), | |
| 'acfw_bulk_reset_sc' | |
| ); | |
| echo '<div class="notice notice-info is-dismissible"><p>'; | |
| echo esc_html__( 'Bulk reset store credits for role(s): ', 'acfw-bulk-reset' ) . '<code>' . esc_html( ACFW_BULK_RESET_DEFAULT_ROLE ) . '</code> — '; | |
| echo '<a href="' . esc_url( $url ) . '">' . esc_html__( 'Run now', 'acfw-bulk-reset' ) . '</a>'; | |
| echo '</p></div>'; | |
| }, 10 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment