wpgenie

Recheck for instant winners

Oct 18th, 2023
1,101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.70 KB | None | 0 0
  1. add_action( 'admin_head', 'custom_add_instant_wins',99);
  2.  
  3. function custom_add_instant_wins(){
  4.     global $product, $wpdb, $post;
  5.  
  6.     $history = false;
  7.  
  8.  
  9.  
  10.     if ( empty( $_REQUEST['lottery-instant-winners-check'] ) ) {
  11.         return;
  12.     }
  13.     if ( 'yes' !== get_post_meta( $post->ID, '_lottery_instant_win', true ) ){
  14.             return;
  15.     }
  16.  
  17.  
  18.  
  19.     $product = wc_get_product( $post->ID);
  20.     $product_id = $post->ID;
  21.  
  22.     if ( $product) {
  23.         $product_id = $post->ID;
  24.         $wheredatefrom = '';
  25.         $datefrom      = FALSE;
  26.  
  27.         $relisteddate = get_post_meta( $product_id, '_lottery_relisted', true );
  28.  
  29.         if($relisteddate){
  30.             $wheredatefrom =" AND CAST(date AS DATETIME) > '$datefrom' ";
  31.         }
  32.  
  33.         $history = $wpdb->get_results( 'SELECT * FROM '.$wpdb->prefix.'wc_lottery_log LEFT JOIN '.$wpdb->prefix.'wc_lottery_pn_log on '.$wpdb->prefix.'wc_lottery_log.id = '.$wpdb->prefix.'wc_lottery_pn_log.log_id WHERE '.$wpdb->prefix.'wc_lottery_log.lottery_id =' . $product_id . $wheredatefrom.' ORDER BY `date` DESC');
  34.  
  35.  
  36.     }
  37.  
  38.     if ( $history ){
  39.         foreach ($history as $value) {
  40.  
  41.             if( wc_lottery_use_answers( $product_id ) && 'yes' === get_option( 'lottery_remove_ticket_wrong_answer' , 'no' ) ){
  42.  
  43.                     $true_answer = false;
  44.                     $true_answers = wc_lottery_pn_get_true_answers( $product_id );
  45.  
  46.                     if( $value->key == __( 'Answer', 'wc-lottery-pn' ) ){
  47.                         $answers_ids  = array_keys( $true_answers );
  48.                         $true_answer = in_array( $value->answer_id, $answers_ids );
  49.                     }
  50.  
  51.                     if ( ! $true_answer ){
  52.                         continue;
  53.                     }
  54.  
  55.             }
  56.  
  57.  
  58.             $ticket_number = apply_filters( 'ticket_number_display_html' , $value->ticket_number, $product ) ;
  59.  
  60.             $instant_ticket_numbers_prizes = maybe_unserialize( get_post_meta( $product_id, '_lottery_instant_ticket_numbers_prizes', true ) );
  61.             if ( ! $instant_ticket_numbers_prizes  ){
  62.                 $instant_ticket_numbers_prizes = wc_lottery_get_lottery_instant_ticket_numbers_prizes_field( $product_id );
  63.             }
  64.             $instant_winning_tickets = array();
  65.  
  66.             if ( ! empty( $instant_ticket_numbers_prizes ) ) {
  67.  
  68.             foreach ( $instant_ticket_numbers_prizes as $key => $instant_winner ) {
  69.  
  70.  
  71.                     if( isset( $instant_winner['ticket'] ) && $instant_winner['ticket'] == $value->ticket_number ){
  72.  
  73.  
  74.                         $data = array(
  75.                             'ticket'     => $instant_winner['ticket'],
  76.                             'user_id'    => $value->userid,
  77.                             'order_id'   => $value->orderid,
  78.                             'prize'      => $instant_winner['prize'],
  79.                             'product_id' => $product_id
  80.                         );
  81.                         $instant_winners = get_post_meta ( $product_id, '_lottery_instant_instant_winners');
  82.  
  83.                         if ( ! in_array( $data , $instant_winners ) ){
  84.  
  85.                             add_post_meta( $product_id, '_lottery_instant_instant_winners', $data );
  86.                         }
  87.  
  88.  
  89.                     }
  90.                 }
  91.             }
  92.  
  93.  
  94.  
  95.         }
  96.     }
  97.  
  98. }
Advertisement
Add Comment
Please, Sign In to add comment