Add a surcharge to cart and checkout – uses fees API

You are here:
This is a Developer level doc. If you are unfamiliar with code/templates, select a WooExpert or Developer for assistance. We are unable to provide support for customizations under our  Support Policy.

 

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 percentage based surcharge to all transactions

  /**
  * 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, ” );
   
  }

 

Add a standard $ value surcharge to all transactions

  /**
  * Add a standard $ value surcharge to all transactions in cart / checkout
  */
  add_action( ‘woocommerce_cart_calculate_fees’,’wc_add_surcharge’ );
  function wc_add_surcharge() {
  global $woocommerce;
   
  if ( is_admin() && ! defined( ‘DOING_AJAX’ ) )
  return;
   
  $county = array(‘US’);
  // change the $fee to set the surcharge to a value to suit
  $fee = 1.00;
   
  if ( in_array( WC()->customer->get_shipping_country(), $county ) ) :
  $woocommerce->cart->add_fee( ‘Surcharge’, $fee, true, ‘standard’ );
  endif;
  }

 

 

 

Add a surcharge based on the delivery country

  /**
  * Add a 1% surcharge to your cart / checkout based on delivery country
  * Taxes, shipping costs and order subtotal are all included in the surcharge amount
  */
  add_action( ‘woocommerce_cart_calculate_fees’,’woocommerce_custom_surcharge’ );
  function woocommerce_custom_surcharge() {
  global $woocommerce;
   
  if ( is_admin() && ! defined( ‘DOING_AJAX’ ) )
  return;
   
  $county = array(‘US’);
  $percentage = 0.01;
   
  if ( in_array( $woocommerce->customer->get_shipping_country(), $county ) ) :
  $surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;
  $woocommerce->cart->add_fee( ‘Surcharge’, $surcharge, true, ” );
  endif;
   
  }

 

Was this article helpful?
Dislike 0
Views: 10