Cart Fees

Do you need to add some type of fee to your shopping cart? An extra line item not associated with a product? In the following article I’ll show you a couple ways this can be accomplished.

[php_tag]/**
 * @snippet       Add Cart Fees Entry
 * @author        Dave Jesch
 * @date-written  Apr 19 2015
 * @date-revised  Sep 1 2019
 * @testedwith    WooCommerce 3.9
 * @donate $5     https://davejesch.com/send-me-coffee/
 */

function d3j_add_checkout_fee()
{
	// Edit [qt]Fee[qt] and [qt]5[qt] below to control Label and Amount
	$fee_name = __('Special Handling Fee', 'text-domain' );
	$fee_amount = 5.25;			// $5.25
	WC()-[gt]cart-[gt]add_fee( $fee_name, $fee_amount );
}
add_action( 'woocommerce_cart_calculate_fees', 'd3j_add_checkout_fee' );

/**
 * @snippet			Add Cart Fee when using PayPal
 * @author			Dave Jesch
 * @testedwith		WooCommerce 3.9
 * @donate $5		https://davejesch.com/send-me-coffee/
 */

function d3j_add_paypal_fee()
{
	$current_gateway = WC()-[gt]session-[gt]get( 'chosen_payment_method' );
	if ( FALSE !== stripos( $current_gateway, 'paypal' ) ) {
		$fee_name = __('PayPal Surcharge', 'text-domain' );
		WC()-[gt]initialize_cart();
		$total = WC()-[gt]cart-[gt]get_cart_contents_total();
		$tax = WC()-[gt]cart-[gt]get_cart_contents_tax();
		// if you don't want to include taxes in calculation, remove $tax from following line
		$cart_total = $total + $tax;
		$fee_amount = 0.30 + ( $cart_total * .029 );		// $0.30 + a 2.9% handling fee for PayPal
		WC()-[gt]cart-[gt]add_fee( $fee_name, $fee_amount );
	}
}
add_action( 'woocommerce_cart_calculate_fees', 'd3j_add_paypal_fee' );

// Part 2: reload checkout on payment gateway change

function d3j_refresh_checkout_on_payment_method_change()
{
?[gt]
	[lt]script type=[qt]text/javascript[qt][gt]
		(function($){
			$( 'form.checkout' ).on( 'change', 'input[name^=[qt]payment_method[qt]]', function() {
				$( 'body' ).trigger( 'update_checkout' );
			});
		})(jQuery);
	[lt]/script[gt]
[php_tag]
}
add_action( 'woocommerce_review_order_before_payment', 'd3j_refresh_checkout_on_payment_method_change' );

The above code includes two examples. The first example adds a simple “Special Handling Fee” of $5.25 to the cart. This has no conditions, it will add the fee to all carts.

The second example adds a fee when a PayPal Payment Processor is selected for the cart. It takes into account the current $0.30 + 2.9% fee that PayPal charges. If you select a different payment processor, the fee is removed. You can change the condition on line 29 to look for a different payment processor if you need to look for something besides PayPal, then change the fee description on line 30.

You can add additional checks to the fees if you like. For example, you could copy the first example and rename it to d3j_subscription_fee(). Then, inside the function check to see if the products are subscription products. If the cart doesn’t contain any subscription products, as long as you don’t call the ->add_fee() method, the fee won’t be added.

Leave a Reply

Your email address will not be published. Required fields are marked *