How to Add a 5% Surcharge to Your WooCommerce Cart / Checkout

# This is post #1 of 31 in the challenge “Mastering WooCommerce

This snippet mostly explained how you can add a 5% surcharge to WooCommerce cart / checkout based on conditions.

Option 1 (Add 5% surcharge for all) :

add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge_all' );

function woocommerce_custom_surcharge_all() {
  global $woocommerce;

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

  $percentage = 0.05; //change this value for surcharge percentage
  $surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;

  $woocommerce->cart->add_fee( 'Surcharge (5%)', $surcharge, true, 'standard' );
}

Option 2 (Add 5% surcharge based on country) :

add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge_country' );

function woocommerce_custom_surcharge_country() {
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
  return;

$county = array('US');
$percentage = 0.05; //change this value to update % / activity
 
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 (5%) *US Only', $surcharge, true, 'standard' );
endif;
}

2 Comments

Leave a Comment.