hookhook-woocommerceadd-action

Serial Number from custom table not appear in woocommerce_email_before_order_table action


I have a custom table with serial numbers in WordPress. I have successfully got the serial number to appear on both Order received page after testing with Stripe: https://prnt.sc/9tz8i3BW7lJR

and it also appears on WooCommerce Admin Orders Page:

https://prnt.sc/jLyb5CQqSAH5

I am using the woocommerce_email_before_order_table action. (on customer_completed_order) I have the code below and I have echoed the Order ID and the Custom TableName and they BOTH appear in the Thanks for shopping with us email. It seems the $license query returns nothing and I just can't see why it won't appear. If I exchange the $woo_order_id for the previous order no, like EMS-0051 the serial number appears. Is this query too early and it hasn't been populated in the custom table before the query is run? I cannot get it to work..can anyone see what I have done wrong, please? The Thanks email and CODE are below. https://prnt.sc/38wa50jTyr3U

    <?php
    
    add_action( 'woocommerce_email_before_order_table', 'add_serial_to_email', 25, 4 ); 
    
    function add_serial_to_email( $order, $sent_to_admin, $plain_text, $email ) {
       global $wpdb;
       $ipn_tables = $wpdb->prefix ."ipn_data_tbl";
    
     ///////BELOW is using 'seq Order No' plugin..this checks if WOO O/N or plugins O/N.
       if (empty($order->get_id))  {      
        $woo_order_id = $order->get_order_number();
          }             
     elseif (empty($order->get_order_number))  {
        $woo_order_id  = $order->get_id();
         }
    
     ///check order ID and Table name are there:
    if (!empty($woo_order_id && $ipn_tables )) {
       echo '<b>ORDER ID:</b>  '.$woo_order_id.'<br>'; // echos the Order ID - appears on "Thanks for shopping with us" email
       echo '<b>TABLE NAME:</b>  '.$ipn_tables.'<br>';   // echo my Custom table name - appears on "Thanks for shopping with us" email
       ////But the below $license variable doesn't. I think it's a timing thing. 
       //$license = $wpdb->get_var(" SELECT serial_no FROM $ipn_tables WHERE woo_order_id = $woo_order_id " );
       $license = $wpdb->get_var( $wpdb->prepare( "SELECT * FROM {$ipn_tables} WHERE woo_order_id = %s", $woo_order_id ) );
          }
    
       if ( $email->id == 'customer_completed_order' ){
      
          printf( '<p class="custom-text">' .__( 'Your Software Serial Number:  '.'<span style="color:red;font-weight:bold;font-size:15px">'.$license ));
       } 
    }//function-END
    ?>

Forgot to show the MyPHPAdmin table:

https://prnt.sc/A4DH1v2STWrL

edit: I should have mentioned that I put that license check for orderID and table just to see if it was being checked..it appears my get_var query isn't working (empty?) but that same query is used in the other PHP pages I edited.


Solution

  • Looks like I found the issue. It was the fact that 'woocommerce_payment_complete' hook was too early BUT the hook 'woocommerce_pre_payment_complete' is called first after payment is made but before order status change and before the email is sent. :) So all I changed in the add_action was change: woocommerce_payment_complete' TO woocommerce_pre_payment_complete that's it. And it worked. part of the add_action updated code