Spin Wheel

⌘K
  1. Home
  2. Spin Wheel
  3. Hooks & Filters (Deve...
  4. After prize is selected (uspw_after_prize_selected)

After prize is selected (uspw_after_prize_selected)

uspw_after_prize_selected

Description

Fires after a prize is selected but before the response is sent to the user. Perfect for notifications, inventory tracking, or custom logging of winning results.

Hook Type: Action
Since: 1.0.0

Parameters

ParameterTypeDescription
$selected_prizearrayThe winning prize details
$campaign_idintThe ID of the spin wheel campaign
$user_dataarrayUser submitted data (email, name, phone, etc.)
$won_codestringThe actual coupon code won

Usage

add_action( 'uspw_after_prize_selected', 'my_prize_handler', 10, 4 );
function my_prize_handler( $selected_prize, $campaign_id, $user_data, $won_code ) {
    // Your custom logic here
}

Real-World Examples

Example 1: Send Real-Time Winner Notifications

Notify your team immediately when someone wins a high-value prize.

add_action( 'uspw_after_prize_selected', 'notify_team_of_winner', 10, 4 );
function notify_team_of_winner( $selected_prize, $campaign_id, $user_data, $won_code ) {
    $prize_label = $selected_prize['label'] ?? 'Unknown Prize';
    $prize_value = floatval( $selected_prize['value'] ?? 0 );
    
    // Only notify for prizes over $25
    if ( $prize_value >= 25 ) {
        // Send to Slack
        $slack_webhook = get_option( 'spin_wheel_slack_webhook' );
        
        if ( $slack_webhook ) {
            wp_remote_post( $slack_webhook, [
                'body' => wp_json_encode( [
                    'text' => sprintf(
                        '🎉 *New Winner!*\n*Prize:* %s ($%s)\n*Winner:* %s\n*Code:* `%s`\n*Campaign:* %d',
                        $prize_label,
                        number_format( $prize_value, 2 ),
                        $user_data['email'] ?? 'Unknown',
                        $won_code,
                        $campaign_id
                    ),
                    'username' => 'Spin Wheel Bot',
                    'icon_emoji' => ':slot_machine:'
                ] ),
                'headers' => [ 'Content-Type' => 'application/json' ]
            ] );
        }
        
        // Also send email to sales team
        wp_mail(
            '[email protected]',
            'High-Value Spin Wheel Winner',
            sprintf(
                "A user just won %s!\n\nDetails:\nEmail: %s\nName: %s\nCoupon: %s\nCampaign ID: %d",
                $prize_label,
                $user_data['email'] ?? 'N/A',
                $user_data['name'] ?? 'N/A',
                $won_code,
                $campaign_id
            )
        );
    }
}

Example 2: Track Prize Inventory

Update custom inventory tracking when unique prizes are won.

add_action( 'uspw_after_prize_selected', 'track_prize_inventory', 10, 4 );
function track_prize_inventory( $selected_prize, $campaign_id, $user_data, $won_code ) {
    global $wpdb;
    
    $table_name = $wpdb->prefix . 'spin_prize_inventory';
    
    // Log the prize distribution
    $wpdb->insert( $table_name, [
        'campaign_id' => $campaign_id,
        'prize_label' => $selected_prize['label'] ?? '',
        'prize_value' => $selected_prize['value'] ?? 0,
        'coupon_code' => $won_code,
        'winner_email' => $user_data['email'] ?? '',
        'won_at' => current_time( 'mysql' ),
        'redeemed' => 0
    ] );
    
    // Check remaining inventory for this prize type
    $prize_id = $selected_prize['id'] ?? 0;
    $remaining = $wpdb->get_var( $wpdb->prepare(
        "SELECT COUNT(*) FROM {$table_name} 
        WHERE campaign_id = %d 
        AND prize_label = %s 
        AND redeemed = 0",
        $campaign_id,
        $selected_prize['label'] ?? ''
    ) );
    
    // Alert if running low
    if ( $remaining < 5 ) {
        wp_mail(
            get_option( 'admin_email' ),
            'Low Prize Inventory Alert',
            sprintf(
                'Prize "%s" in campaign %d has only %d remaining codes.',
                $selected_prize['label'] ?? 'Unknown',
                $campaign_id,
                $remaining
            )
        );
    }
}

Example 3: Trigger Marketing Automation

Start automated email sequences based on the prize won.

add_action( 'uspw_after_prize_selected', 'trigger_email_automation', 10, 4 );
function trigger_email_automation( $selected_prize, $campaign_id, $user_data, $won_code ) {
    if ( empty( $user_data['email'] ) ) {
        return;
    }
    
    $prize_value = floatval( $selected_prize['value'] ?? 0 );
    
    // Mailchimp integration
    $mailchimp_api_key = get_option( 'mailchimp_api_key' );
    $list_id = get_option( 'mailchimp_list_id' );
    
    if ( $mailchimp_api_key && $list_id ) {
        $dc = substr( $mailchimp_api_key, strpos( $mailchimp_api_key, '-' ) + 1 );
        $endpoint = "https://{$dc}.api.mailchimp.com/3.0/lists/{$list_id}/members";
        
        // Assign to different segments based on prize value
        $tags = [ 'spin-wheel-winner' ];
        
        if ( $prize_value >= 50 ) {
            $tags[] = 'high-value-winner';
            $tags[] = 'vip-segment';
        } elseif ( $prize_value >= 20 ) {
            $tags[] = 'medium-value-winner';
        } else {
            $tags[] = 'low-value-winner';
        }
        
        wp_remote_post( $endpoint, [
            'headers' => [
                'Authorization' => 'Basic ' . base64_encode( 'user:' . $mailchimp_api_key ),
                'Content-Type' => 'application/json'
            ],
            'body' => wp_json_encode( [
                'email_address' => $user_data['email'],
                'status' => 'subscribed',
                'merge_fields' => [
                    'FNAME' => $user_data['name'] ?? '',
                    'PHONE' => $user_data['phone'] ?? '',
                    'COUPON' => $won_code,
                    'PRIZEVALUE' => $prize_value
                ],
                'tags' => $tags
            ] )
        ] );
    }
}

Example 4: Create Custom Post for Each Winner

Generate a “Winner” custom post type entry for display on your site.

add_action( 'uspw_after_prize_selected', 'create_winner_post', 10, 4 );
function create_winner_post( $selected_prize, $campaign_id, $user_data, $won_code ) {
    // Only create posts for prizes over $10
    $prize_value = floatval( $selected_prize['value'] ?? 0 );
    
    if ( $prize_value < 10 ) {
        return;
    }
    
    // Create winner post
    $post_id = wp_insert_post( [
        'post_title' => sprintf(
            '%s won %s',
            $user_data['name'] ?? 'A lucky customer',
            $selected_prize['label'] ?? 'a prize'
        ),
        'post_type' => 'spin_winner',
        'post_status' => 'publish',
        'post_content' => sprintf(
            'Congratulations to %s for winning %s in our spin wheel campaign!',
            $user_data['name'] ?? 'our winner',
            $selected_prize['label'] ?? 'an amazing prize'
        )
    ] );
    
    if ( $post_id ) {
        // Add meta data
        update_post_meta( $post_id, 'winner_email', $user_data['email'] ?? '' );
        update_post_meta( $post_id, 'prize_label', $selected_prize['label'] ?? '' );
        update_post_meta( $post_id, 'prize_value', $prize_value );
        update_post_meta( $post_id, 'coupon_code', $won_code );
        update_post_meta( $post_id, 'campaign_id', $campaign_id );
        update_post_meta( $post_id, 'won_date', current_time( 'mysql' ) );
        
        // Optionally set featured image (confetti or prize image)
        $prize_image_id = $selected_prize['image_id'] ?? 0;
        if ( $prize_image_id ) {
            set_post_thumbnail( $post_id, $prize_image_id );
        }
    }
}

Example 5: Sync with Analytics Platforms

Send conversion events to Google Analytics, Facebook Pixel, etc.

add_action( 'uspw_after_prize_selected', 'track_conversion_events', 10, 4 );
function track_conversion_events( $selected_prize, $campaign_id, $user_data, $won_code ) {
    $prize_value = floatval( $selected_prize['value'] ?? 0 );
    $prize_label = $selected_prize['label'] ?? 'Unknown Prize';
    
    // Google Analytics 4 Event
    if ( function_exists( 'gtag' ) ) {
        gtag( 'event', 'spin_wheel_win', [
            'event_category' => 'Spin Wheel',
            'event_label' => $prize_label,
            'value' => $prize_value,
            'currency' => 'USD',
            'campaign_id' => $campaign_id,
            'coupon_code' => $won_code
        ] );
        
        // Also track as a conversion
        gtag( 'event', 'conversion', [
            'send_to' => 'AW-CONVERSION_ID/CONVERSION_LABEL',
            'value' => $prize_value,
            'currency' => 'USD',
            'transaction_id' => $won_code
        ] );
    }
    
    // Facebook Pixel Event
    if ( function_exists( 'fbq' ) ) {
        ?>
        <script>
        fbq('track', 'SpinWheelWin', {
            content_name: '<?php echo esc_js( $prize_label ); ?>',
            value: <?php echo esc_js( $prize_value ); ?>,
            currency: 'USD',
            content_ids: ['<?php echo esc_js( $won_code ); ?>']
        });
        </script>
        <?php
    }
    
    // Send to custom analytics endpoint
    wp_remote_post( 'https://analytics.yourcompany.com/api/events', [
        'body' => wp_json_encode( [
            'event_type' => 'spin_wheel_win',
            'user_email' => $user_data['email'] ?? '',
            'prize_label' => $prize_label,
            'prize_value' => $prize_value,
            'coupon_code' => $won_code,
            'campaign_id' => $campaign_id,
            'timestamp' => current_time( 'timestamp' )
        ] ),
        'headers' => [
            'Content-Type' => 'application/json',
            'Authorization' => 'Bearer ' . get_option( 'analytics_api_key' )
        ]
    ] );
}

Notes

  • This hook fires after the prize is selected and the coupon code is assigned, but before the response is sent to the user.
  • Perfect for real-time notifications, inventory management, and external integrations.
  • The $selected_prize array contains all prize data including label, value, probability, and custom fields.
  • The $won_code parameter contains the actual coupon code that was assigned (important for unique code tracking).

How can we help?