Automatically adding a transaction fee or transaction ID to WooCommerce orders

How to add a transaction fee or transaction ID to Woocommerce orders automatically in order to facilitate automatically recording and syncing transaction fees with WooCommerce orders

Written by the MyWorks Team

Updated at June 27th, 2023

One of the helpful features of popular WooCommerce gateways, like Stripe & PayPal, is to add the transaction's ID and transaction fee as metadata within the WooCommerce order. In addition to being most transparent, this allows other platforms that pull data from WooCommerce - like MyWorks Sync - to be able to leverage this data to accurately add transaction IDs and transaction fees in other platforms, like QuickBooks Online or QuickBooks Desktop. When a WooCommerce gateway correctly records this data in a WooCommerce order, the order can look like this example:

Unfortunately, some WooCommerce payment gateways don't include these default fields when creating orders in WooCommerce. While this is something that the payment gateway developers can easily resolve, it's sometimes not as fast or a priority of a fix as our users sometimes hope.

So, as an alternative, our team at MyWorks has built a standalone hook to allow our users to easily accomplish the above - including a transaction ID and/or transaction fee with WooCommerce orders. This helps circumvent the WooCommerce gateway and automatically adds this data to the WooCommerce order as it's being placed. 


Simply providing this doc or the hook below to your WooCommerce developer, and asking them to add to your site is all that's necessary! 


  • This is commonly added to your active theme's functions.php file, but can be added elsewhere in your site as well.
  • Set the names of your WooCommerce gateways you'd like this to apply to in the appropriate spot in the function.
  • Set the desired % and $ transaction fee in the appropriate spot in the function.

Benefit when using MyWorks

When using MyWorks Sync, and the below hook, WooCommerce orders placed in your store will contain a transaction ID and transaction fee value. With this information, MyWorks can accurately sync an order to QuickBooks as paid, if so desired.


// Built by MyWorks Software -
// Adds transaction ID and/or transaction fee to a WooCommerce order
// when placed, if not automatically added by the WooCommerce payment gateway.
// Built for use with MyWorks Sync for QuickBooks - but applicable
// in any scenario where a transaction ID and/or transaction fee is needed in an order.

add_action( 'woocommerce_new_order', 'myworks_action_woocommerce_new_order', 10, 1 ); 
function myworks_action_woocommerce_new_order( $order_id ) { 
	$applicable_gateways = array(
		'Direct bank transfer',
		'Check payments',
		'Cash on delivery'
		//The gateways set here will determine which gateways this logic applies to. Add/remove as desired.
	//Set the transaction fee percentage your processor charges; ex 2.9%
	$transaction_percent = '2.9';
	//Set the transaction fee $ amount your processor charges for each transaction; ex $0.30
	$transaction_fee = '0.30';

	$order = wc_get_order( $order_id );

	do_action('myworks_woocommerce_order_transaction_details',$order, $applicable_gateways,$transaction_percent, $transaction_fee);	   

add_action( 'myworks_woocommerce_order_transaction_details', 'myworks_add_transaction_fee_woocommerce_order', 10, 4 );

function myworks_add_transaction_fee_woocommerce_order( $order, $applicable_gateways = array(), $transaction_percent, $transaction_fee  ){
	$payment_title = $order->get_payment_method_title();
	$payment_title_x = $order->get_payment_method();
	if(in_array($payment_title, $applicable_gateways)){
		$order_data = $order->get_data();
		$order_total = $order_data['total'];
		$transaction_fee_amount = ($order_total * $transaction_percent)/100;
		$transaction_fee_amount = $transaction_fee_amount + $transaction_fee;
		$transaction_fee_amount = round($transaction_fee_amount,2);

		add_post_meta($order->id, '_transaction_id', date('Ymdhis'));
		add_post_meta($order->id,'_transaction_fee', $transaction_fee_amount);