wpgenie

Lottery history tab template sorted by ticket number

Oct 18th, 2021
745
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /**
  3.  * Lottery history tab template sorted by ticket number
  4.  *
  5.  * This template can be overridden by copying it to yourtheme/woocommerce/single-product/tabs/lottery-history.php
  6.  *
  7.  */
  8. if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
  9. global $woocommerce, $post, $product;
  10.  
  11. $lottery_winers = get_post_meta($post->ID, '_lottery_winners');
  12. $users_names = '';
  13. $use_answers = wc_lottery_use_answers( $post->ID );
  14. $use_ticket_numbers = get_post_meta( $post->ID , '_lottery_use_pick_numbers', true );
  15. $answers = maybe_unserialize( get_post_meta( $post->ID, '_lottery_pn_answers', true ) );
  16. $date_format = get_option( 'date_format' );
  17. $time_format = get_option( 'time_format' );
  18.  
  19. ?>
  20.  
  21. <h2><?php echo esc_html( __( 'Lottery History', 'wc-lottery-pn' ) ); ?></h2>
  22.  
  23.  
  24. <?php if(($product->is_closed() === TRUE ) and ($product->is_started() === TRUE )) : ?>
  25.    
  26.     <p><?php _e('Lottery has finished', 'wc-lottery-pn') ?></p>
  27.     <?php if ($product->get_lottery_fail_reason() == '1'){
  28.          _e('Lottery failed because there were no minimal number of participants', 'wc-lottery-pn');
  29.     } else{
  30.  
  31.         if ( count($lottery_winers) > 1 ) { ?>
  32.  
  33.            <p><?php _e('Lottery winners are', 'wc-lottery-pn') ?>: <?php foreach ($lottery_winers as $winner_id) {
  34.             if( intval( $winner_id ) > 0) {
  35.                     $users_names .= "<span>";
  36.                     $users_names .= get_userdata($winner_id)->display_name;
  37.                     $users_names .= "</span>, ";
  38.                 }
  39.             } ?><?php echo rtrim( $users_names , ', '); ?></p>
  40.        
  41.         <?php } elseif( count($lottery_winers) === 1 ) {
  42.             $winner = reset($lottery_winers);
  43.  
  44.             if ( ! empty ( $winner ) ) { ?>
  45.             <p><?php _e('Lottery winner is:', 'wc-lottery-pn') ?> <span><?php echo get_userdata($winner)->display_name ?></span></p>
  46.         <?php }
  47.         }
  48.     } ?>
  49.                            
  50. <?php endif; ?>
  51.  
  52. <table>
  53.     <thead>
  54.         <tr>
  55.             <th><?php _e('Date', 'wc-lottery-pn') ?></th>
  56.             <th><?php _e('User', 'wc-lottery-pn') ?></th>
  57.             <?php if ($use_ticket_numbers === 'yes' ) :?>
  58.                 <th><?php _e('Ticket number', 'wc-lottery-pn') ?></th>
  59.             <?php endif; ?>
  60.             <?php if ($use_answers === true && 'yes' === get_option('lottery_answers_in_history', 'yes')  && ( 'no' === get_option('lottery_answers_in_history_finished', 'no') || $product->is_closed() === TRUE ) ) :?>
  61.                 <th><?php _e('Answer', 'wc-lottery-pn') ?></th>
  62.             <?php endif; ?>
  63.         </tr>
  64.     </thead>
  65.     <?php
  66.         global $wpdb;
  67.         $wheredatefrom = '';
  68.         $datefrom      = FALSE;
  69.  
  70.         $relisteddate = get_post_meta( $post->ID, '_lottery_relisted', true );
  71.         if(!empty($relisteddate)){
  72.             $datefrom = $relisteddate;
  73.         }
  74.  
  75.         if($datefrom){
  76.             $wheredatefrom =" AND CAST(date AS DATETIME) > '$datefrom' ";
  77.         }
  78.  
  79.         $lottery_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 =' . $post->ID . $wheredatefrom.' ORDER BY `ticket_number` ASC');
  80.  
  81.         if( $lottery_history ) {         
  82.        
  83.             foreach ($lottery_history as $history_value) {
  84.  
  85.                 echo "<tr>";
  86.                 echo "<td class='date'>".date_i18n( $date_format, strtotime( $history_value->date )).' '.date_i18n( $time_format, strtotime( $history_value->date ))."</td>";
  87.                 echo $history_value->userid ? "<td class='username'>". ( get_userdata($history_value->userid) ? get_userdata($history_value->userid)->display_name : '' ) ."</td>" : '';
  88.  
  89.                 if ($use_ticket_numbers === 'yes' ) {
  90.                     echo "<td class='ticket_number'>" . apply_filters( 'ticket_number_display_html' , $history_value->ticket_number, $product ) . "</td>";
  91.                 }
  92.                
  93.                 if ( $use_answers === true && 'yes' === get_option('lottery_answers_in_history', 'yes')  && ( 'no' === get_option('lottery_answers_in_history_finished', 'no') || $product->is_closed() === TRUE ) ){
  94.                     $answer = isset( $answers[$history_value->answer_id] ) ? $answers[$history_value->answer_id] : false;
  95.                    
  96.                     echo "<td class='answer'>";
  97.                     echo $answer !== true ? $answer['text'] : "" ;
  98.                     echo "</td>";
  99.                 }
  100.                
  101.                 echo "</tr>";
  102.             }
  103.        
  104.         }
  105.     ?> 
  106.     <tr class="start">
  107.             <?php
  108.            
  109.             $lottery_dates_to = $product->get_lottery_dates_from();
  110.  
  111.  
  112.             if ($product->is_started() === TRUE ){
  113.                 echo '<td class="date">'.date_i18n( $date_format,  strtotime( $lottery_dates_to )).' '.date_i18n( $time_format,  strtotime( $lottery_dates_to )).'</td>';              
  114.                 echo '<td class="started">';
  115.                 echo apply_filters('auction_history_started_text', __( 'Lottery started', 'wc-lottery-pn' ), $product);
  116.                 echo '</td>';
  117.  
  118.             } else {
  119.                 echo '<td class="date">'.date_i18n( $date_format,  strtotime( $lottery_dates_to )).' '.date_i18n( $time_format,  strtotime( $lottery_dates_to )).'</td>';              
  120.                 echo '<td class="starting">';
  121.                 echo apply_filters('auction_history_starting_text', __( 'Lottery starting', 'wc-lottery-pn' ), $product);
  122.                 echo '</td>' ;
  123.             }?>
  124.     </tr>
  125. </table>
RAW Paste Data