Home / eCommerce / Allow Admin to Redeem Customer’s Loyalty Points to Store Credits
Duplicate Snippet

Embed Snippet on Your Site

Allow Admin to Redeem Customer’s Loyalty Points to Store Credits

1. Add it to your site using one of the following methods:
- Option A: Add it to your child theme's functions.php file (recommended for developers).
- Option B: Use a custom plugin for snippets (like Code Snippets) and paste the code there.
Then, save and activate the snippet.

2. It creates a custom AJAX action (migrate_loyalty_to_credits) that:
- Loops through all loyalty points entries.
- Calculates remaining points per user (earned minus redeemed).
- Converts points to store credits using the points-to-credits conversion ratio (acfw_loyalprog_cost_points_ratio).
- Inserts store credits into the acfw_store_credits table.
- Marks the loyalty points as redeemed (to prevent duplicate conversion).
- Stores a flag in user meta to track migrated credits.

3. After adding the snippet, you need to call the AJAX action. Two ways to do it:
- Option A: Use CURL (from the snippet)
Run this command in your terminal (replace acfwp.local and credentials with your site info):

curl -X POST "https://acfwp.local/wp-admin/admin-ajax.php"
-u "admin:yourpassword"
-d "action=migrate_loyalty_to_credits"

Option B: Use the Browser (Quick Method)
While logged in as an admin, go to: https://acfwp.local/wp-admin/admin-ajax.php?action=migrate_loyalty_to_credits
This will execute the migration and return a JSON response.

4. After that, check the response for:
message: should say "Loyalty points successfully migrated to store credits."
migrated_users: shows user IDs, loyalty points converted, and store credits.

You can also verify in the database:
Table: wp_acfw_store_credits
Look for new entries with entry_action = 'loyalty_points'.

5. Important Notes:
- Backup before running this: It modifies your database by adding store credits and marking points as redeemed.
- This runs once per user; it won’t duplicate credits because of the acfw_imported_store_credits_from_lpfw meta check.
- Conversion ratio is pulled from plugin settings (acfw_loyalprog_cost_points_ratio).

Code Preview
php
<?php
add_action('wp_ajax_migrate_loyalty_to_credits', function() {
    // Only allow admin users
    if (!current_user_can('manage_options')) {
        wp_send_json_error('Unauthorized access', 403);
    }
    global $wpdb;
    $loyalty_table = $wpdb->prefix . 'acfw_loyalprog_entries';
    $credits_table = $wpdb->prefix . 'acfw_store_credits';
    // Get the points-to-store-credits conversion ratio
    $conversion_ratio = floatval(get_option('acfw_loyalprog_cost_points_ratio', 1));
    // Step 1: Get total earned points per user
    $earn_entries = $wpdb->get_results("
        SELECT user_id, SUM(entry_amount) AS total_earn
        FROM $loyalty_table
        WHERE entry_type = 'earn'
        GROUP BY user_id
    ", OBJECT_K);
    // Step 2: Get total redeemed points per user
    $redeem_entries = $wpdb->get_results("
        SELECT user_id, SUM(entry_amount) AS total_redeem
        FROM $loyalty_table
        WHERE entry_type = 'redeem'
        GROUP BY user_id
    ", OBJECT_K);
    if (empty($earn_entries)) {
        wp_send_json_error('No earn entries found.');
    }
    $migrated_users = [];
    foreach ($earn_entries as $user_id => $earn_entry) {
        $user_id = (int) $user_id;
        $total_earn = floatval($earn_entry->total_earn);
        $total_redeem = isset($redeem_entries[$user_id]) ? floatval($redeem_entries[$user_id]->total_redeem) : 0.0;
        $remaining_points = $total_earn - $total_redeem;
        $store_credits = $remaining_points * $conversion_ratio;
        // Check if migration is needed
        $imported_store_credits = floatval(get_user_meta($user_id, 'acfw_imported_store_credits_from_lpfw', true));
        if ($remaining_points <= 0 || $imported_store_credits >= $store_credits) {
            continue;
        }
        // Step 3: Insert converted credits into store credits table
        $wpdb->insert($credits_table, [
            'user_id'       => $user_id,
            'entry_date'    => current_time('mysql'),
            'entry_type'    => 'increase',
            'entry_action'  => 'loyalty_points',
            'entry_amount'  => $store_credits,
            'object_id'     => 0,
            'entry_note'    => '',
        ]);
        // Step 4: Insert redeem record in loyalty table (to close the loop)
        $wpdb->insert($loyalty_table, [
            'user_id'       => $user_id,
            'entry_date'    => current_time('mysql'),
            'entry_type'    => 'redeem',
            'entry_action'  => 'store_credits',
            'entry_amount'  => $remaining_points,
            'object_id'     => 0,
            'entry_notes'   => '',
        ]);
        // Step 5: Save migration flag
        update_user_meta($user_id, 'acfw_imported_store_credits_from_lpfw', $store_credits);
        $migrated_users[] = [
            'user_id' => $user_id,
            'loyalty_points' => $remaining_points,
            'store_credits' => $store_credits,
        ];
    }
    wp_send_json_success([
        'message' => 'Loyalty points successfully migrated to store credits.',
        'migrated_users' => $migrated_users,
        'conversion_ratio' => $conversion_ratio,
    ]);
});

Comments

Add a Comment