Adding Discounts & Fees in WooCommerce

How to add fees and discounts at the checkout in WooCommerce

Adding fees in WooCommerce

Here is a snippet of code to your child theme’s functions.php file or via a plugin that allows custom functions to be added. Do not add this custom code directly to your parent theme’s functions.php file as this will be removed when you update the theme.

Add a percentage based fee at the checkout to all transactions

add_action( ‘woocommerce_cart_calculate_fees’, ‘toast_woocommerce_custom_fee’ );
function toast_woocommerce_custom_fee() {
global $woocommerce;

if (is_admin() && ! defined( ‘DOING_AJAX’ ) )
return;

$percentage = 0.05;
$fee = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;
$woocommerce->cart->add_fee( ‘Fee’, $fee, true, ” );
}

Add a fixed fee at the checkout to all transactions

add_action( ‘woocommerce_cart_calculate_fees’, ‘toast_woocommerce_custom_fee’ );
function toast_woocommerce_custom_fee() {
global $woocommerce;

if (is_admin() && ! defined( ‘DOING_AJAX’ ) )
return;

$fee = 5;
$woocommerce->cart->add_fee( ‘Fee’, $fee, true, ” );
}

Add a percentage discount at the checkout to all transactions

add_action( ‘woocommerce_cart_calculate_fees’, ‘toast_woocommerce_custom_discount’ );
function toast_woocommerce_custom_discount() {
global $woocommerce;

if (is_admin() && ! defined( ‘DOING_AJAX’ ) )
return;

$percentage = 0.5;
$discount = -( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;
$woocommerce->cart->add_fee( ‘Discount’, $discount, true, ”);
}

Menu