wpgenie

start at 1001-3000 - change ticket number +1000

Feb 7th, 2022 (edited)
640
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.84 KB | None | 0 0
  1.  
  2. add_action( 'template_redirect', 'custom_use_alphabet', 100 );
  3. add_action( 'admin_init', 'custom_use_alphabet', 100 );
  4. add_action( 'woocommerce_product_options_lottery', 'custom_use_alphabet', 100 );
  5. add_action( 'export_lottery_history_with_extra_info', 'custom_use_alphabet', 100 );
  6.  
  7. function custom_use_alphabet( $post_id = false ) {
  8.     global $wc_lottery_pn;
  9.  
  10.     remove_filter( 'woocommerce_get_item_data', array( $wc_lottery_pn->plugin_public, 'change_cart_ticket_number_to_alphabet'), 10 );
  11.     remove_filter( 'woocommerce_display_item_meta', array( $wc_lottery_pn->plugin_public, 'change_order_ticket_number_to_alphabet'), 90 );
  12.     remove_filter( 'woocommerce_order_item_display_meta_value', array( $wc_lottery_pn->plugin_public, 'woocommerce_order_item_display_meta_value_aplhabet'), 90 );
  13.     add_filter( 'woocommerce_get_item_data', 'custom_change_cart_ticket_number_to_alphabet', 10, 2 );
  14.     add_filter( 'woocommerce_display_item_meta', 'custom_change_order_ticket_number_to_alphabet', 90, 3 );
  15.     add_filter( 'woocommerce_order_item_display_meta_value', 'custom_woocommerce_order_item_display_meta_value_aplhabet', 90, 3 );
  16.  
  17.  
  18.     if ( is_product() || is_admin() ) {
  19.         $post_id = !$post_id ? get_the_ID() : $post_id;
  20.         if ( 'yes' === get_post_meta( $post_id , '_lottery_pick_number_alphabet', true ) ) {
  21.             remove_filter( 'ticket_number_display_html', array( $wc_lottery_pn->plugin_public, 'change_ticket_numbers_to_alphabet'), 10 );
  22.             remove_filter( 'ticket_number_tab_display_html', array( $wc_lottery_pn->plugin_public, 'change_ticket_tab_to_alphabet'), 10 );
  23.             add_filter( 'ticket_number_display_html', 'custom_change_ticket_numbers_to_alphabet', 10, 2 );
  24.             add_filter( 'ticket_number_tab_display_html', 'custom_change_ticket_numbers_to_alphabet', 10, 2 );
  25.         }
  26.     }
  27. }
  28.  
  29.  
  30. function custom_change_ticket_numbers_to_alphabet( $ticket_number, $product ) {
  31.  
  32.     $ticket_number = intval($ticket_number) + 1000;
  33.  
  34.     return $ticket_number ;
  35. }
  36.  
  37. function custom_change_cart_ticket_number_to_alphabet( $item_data, $cart_item ) {
  38.     // Format item data ready to display.
  39.     foreach ( $item_data as $key => $data ) {
  40.         if ( isset( $data['key'] ) && 'Ticket number' === $data['key'] ) {
  41.             $product = wc_get_product( $cart_item['product_id'] );
  42.             if ( 'yes' === get_post_meta( $cart_item['product_id'] , '_lottery_pick_number_alphabet', true ) ) {
  43.                 $item_data[ $key ]['display'] = custom_change_ticket_numbers_to_alphabet($data['value'], $product );
  44.             }
  45.         }
  46.     }
  47.     return $item_data;
  48. }
  49.  
  50. function custom_change_order_ticket_number_to_alphabet( $html, $item, $args ) {
  51.     $strings    = false;
  52.     $product_id = $item->get_product_id();
  53.     $product    =  wc_get_product( $product_id );
  54.  
  55.     if ( ! $product || 'yes' !== get_post_meta( $product_id , '_lottery_pick_number_alphabet', true ) ) {
  56.         return $html;
  57.     }
  58.     foreach ( $item->get_formatted_meta_data() as $meta_id => $meta ) {
  59.         if ( 'Ticket number' === $meta->key  ) {
  60.             $value     = $args['autop'] ? wp_kses_post( $meta->display_value ) : wp_kses_post( make_clickable( custom_change_ticket_numbers_to_alphabet(intval( $meta->value ), $product )) );
  61.             $strings[] = $args['label_before'] . wp_kses_post( $meta->display_key ) . $args['label_after'] . $value;
  62.         }
  63.     }
  64.  
  65.     if ( $strings ) {
  66.         $html = $args['before'] . implode( $args['separator'], $strings ) . $args['after'];
  67.     }
  68.  
  69.     return $html;
  70. }
  71.  
  72. function custom_woocommerce_order_item_display_meta_value_aplhabet(  $meta_value, $meta, $item ){
  73.  
  74.     if ( is_a($item, 'WC_Order_Item_Product') ) {
  75.  
  76.         $product_id = $item->get_product_id();
  77.         $product =  wc_get_product( $product_id );
  78.  
  79.         if ( ! $product || 'yes' !== get_post_meta( $product_id , '_lottery_pick_number_alphabet', true ) ) {
  80.             return $meta_value;
  81.         }
  82.         if ( 'Ticket number' === $meta->key ) {
  83.                 $meta_value =  custom_change_ticket_numbers_to_alphabet(intval( $meta_value ), $product );
  84.         }
  85.     }
  86.     return $meta_value;
  87. }
  88.  
Advertisement
Add Comment
Please, Sign In to add comment