How to Add Custom Fee Dynamically to Products in Your E-commerce Store

Jaymine Shah
Techcompose
Published in
2 min readDec 11, 2018

E-Commerce Website Development or Woocommerce Website Development is quite popular nowadays. Sometimes you have some own rules for applying custom fees and you can’t use any plugin or any shipping rates because of it may be more complex. Shipping rates have their own limitations, and plugins for adding custom fees may not cover your requirements. This blog will cover how you can add your customs fee correctly and easy way. Here we will see how we will add fee dynamically based on quantity as well price. It will mainly use while you are applying custom taxes like transportation cost, handling the cost, or labor charges etc. We can simply use it with woocommerce_cart_calculate_fees WooCommerce filter, which is used to calculate your total price over shipping charges, surcharge, discount or any custom rates or fee.

Simple Usage:

Filter: woocommerce_cart_calculate_fees

function woocommerce_cart_custom_fee() {
if(WC()->cart->total < 25){ //Check for cart total is < 25
$custom_fee = WC()->cart->total + 5; // Here $5 will add as custom fee
} else {
$custom_fee = WC()->cart->total; //If cart total is > 25 it will not add custom fee
}
$handling_cost = $custom_fee / 2; //Adding Handling Cost
$fee = $custom_fee + $handling_cost; // Custom Fee + Handling Cost WC()->cart->add_fee('Transport Fee:', $fee); //It will add fee
}
add_action('woocommerce_cart_calculate_fees','woocommerce_cart_custom_fee');

Here you can use your own rules to adding a fee, above example is just for explaining how custom fee works dynamically.

The number of online shoppers is increasing day by day globally, any business can grow by expanding their target from the local market to operate globally. Easy internet and attractive offers and feature of online store attract more and more customers to your online portal. Get a dynamic e-commerce portal from reputed E-commerce Development Company for your business having all functionality with customizing features according to your business needs. Contact us to hire WordPress developer or reach us at inquiry@techcompose.com to discuss your e-commerce project requirement.

--

--