Payments: Retrieve Saved Payment Method from Custom WC Orders Table

Retrieves saved payment method data from a custom WooCommerce Orders table.

Important: Always test customizations to your site on a staging environment before making changes to your live / production site.  If you are not experienced with testing changes on a staging site, here is a good article to review.

 
/**
* Retrieves the Payment Method from the custom WooCommerce Orders table.
* Uses @see WC_Order_Data_Store_Custom_Table::get_order_data_from_table()
*
* @param array $payment_data The current payment data.
* @param int $order_id The WooCommerce Order ID
* @return array The payment info.
*/
function xx_patch_wc_custom_tables_plugin_payment_info( $payment_data, $order_id ){

  // Get the WC Order
  $order = wc_get_order( $order_id );

  // Create an instance and pull the data from the custom table.
  $wc_custom_table = new WC_Order_Data_Store_Custom_Table();
  $data = $wc_custom_table->get_order_data_from_table( $order );

  // Get the Gateway ID from the order in the Custom woocommerce orders table..
  $payment_method_id = $data['payment_method'];

  // Retrieve the array of valid Autoship Gateways.
  $valid_methods = autoship_get_valid_payment_methods();

  $payment_data = null;
  if ( isset( $valid_methods[$payment_method_id] ) ){

    // Call the custom order payment data function if it exists.
    $function = "autoship_get_{$valid_methods[$payment_method_id]}_order_payment_data";

    if ( function_exists( $function ) )
    $payment_data = $function( $order_id );

  }

  return $payment_data;

}
add_filter( 'autoship_order_payment_data', 'xx_patch_wc_custom_tables_plugin_payment_info', 10, 2 );