Stuart Lambon

Setting a store-wide percentage for PayPal Adaptive Payments on WooCommerce

The WooCommerce PayPal Adaptive Payments plugin is a paid-for WooCommerce extension that allows you to split your PayPal payments between one or more receivers; it supports chained, parallel and simple payments. The extension by default allows you to set these fees on a product-by-product basis but this is a rather laborious and ineffective way of doing things if your store has many products and your fee is always to be the same.

Luckily, by using the woocommerce_paypal_ap_payment_args filter you can dynamically assign receivers. The code provided on the documentation page allows you to set up multiple receivers across all products and this solves the problem should you have a single set of fees in mind for your store. This initially seems like a good solution but I can imagine that most people would rather work on a percentage-based system. This recently became an issue within a store that I was working on and so by using the same filter I wrote some extra code to allow you to set a store-wide percentage on all orders.

Here’s the code—this only generates a percentage for one extra receiver but could be adapted for more:

function custom_woocommerce_paypal_ap_payment_args( $args, $order ) {
	
	// Get totals for 2 receivers	 
	$total = $order->order_total;
	$receiverTotal = $total * 0.06; // 6% of total
	$receiverTotalRounded = number_format($receiverTotal, 2, '.', ''); // Rounded to 2 decimal places for pence
	
	// Convert to string
	$totalString = strval($total);
	$receiverTotalString = strval($receiverTotalRounded);
	
	// Create receivers
	$args['receiverList'] = array(
		'receiver' => array(
			array(
				'amount' => $totalString,
				'email'  => 'your@email.com',
				// 'primary' => 'true' // use this only for Chained Payment
			),
			array(
				'amount' => $receiverTotalString,
				'email'  => 'receiver1@email.com'
			)
		)
	);
	return $args;
}
add_filter( 'woocommerce_paypal_ap_payment_args', 'custom_woocommerce_paypal_ap_payment_args', 10, 2 );

It works by getting the order total at checkout, working out the percentage of that (with some rounding and formatting for PayPal) and then allocating the fees to each receiver.

Just replace the 0.06 to create your percentage (0.1 for 10%, 0.15 for 15% and so on…), replace the email addresses with yours and the receiver addresses (your PayPal accounts) and add to your WordPress theme’s functions.php file. Obviously I’d recommend testing this heavily on PayPal sandbox before pushing onto a live site.