How to Add a surcharge with WooCommerce

How to Add a surcharge with WooCommerce: If you are a WooCommerce user this great snippet will add a 1% surcharge to your cart on checkout.

You can easily change the $percentage variable to adjust the surcharge percent and you are good to go.

How to Add a surcharge with WooCommerce. Add code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Code snippets plugin. Avoid adding custom code directly to your parent theme’s functions.php file as this will be wiped entirely when you update the theme.

/**
 * Add a 1% surcharge to your cart / checkout
 * change the $percentage to set the surcharge to a value to suit
 */
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge() {
  global $woocommerce;

	if ( is_admin() && ! defined( 'DOING_AJAX' ) )
		return;

	$percentage = 0.01;
	$surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;	
	$woocommerce->cart->add_fee( 'Surcharge', $surcharge, true, '' );

}

 

( WordPress codex functions, hooks, in this snippet: )

add_actionis_admin,


Related Videos:

 


Related Posts:

How to Define a percent surcharge by delivery country with WooCommerce

How to Update: Automatically create media_buttons for shortcode selection

Create a PayPal Donation Shortcode – WordPress

Using the Current Year in Your Posts – WordPress

What is the use of New Keyword in VB.NET?

Learn Python Lists

Change the Author Permalink Structure

How To Start a Blog – Beginner’s Guide for 2020

What is a Shortcode in WordPress?

The background-color CSS property

What is the Python Software Foundation?

Python 3 Object Oriented Programming

B0r0nt0K Ransomware Wants $75,000 Ransom, Infects Linux Servers

Are there copyright restrictions on the use of Python?

How to Spy on Competitors with Python & Data Studio

Kodi-04-2019 No Limits Wizard Magic Build for Kodi 18 Leia

Windows 10 Activator Key, Activate Windows 10 with a batch file

 

 

Connected through code, Choose Your Platform!

About the Author: Bernard Aybout

In the land of bytes and bits, a father of three sits, With a heart for tech and coding kits, in IT he never quits. At Magna's door, he took his stance, in Canada's wide expanse, At Karmax Heavy Stamping - Cosma's dance, he gave his career a chance. With a passion deep for teaching code, to the young minds he showed, The path where digital seeds are sowed, in critical thinking mode. But alas, not all was bright and fair, at Magna's lair, oh despair, Harassment, intimidation, a chilling air, made the workplace hard to bear. Management's maze and morale's dip, made our hero's spirit flip, In a demoralizing grip, his well-being began to slip. So he bid adieu to Magna's scene, from the division not so serene, Yet in tech, his interest keen, continues to inspire and convene.