0

I would like to add handling fee on cart/checkout page with options just like the shipping options. Where customer can choose their packaging type from multiple options.

Here is an example of the frontend. example output

I tried to trigger wc_update_cart, but seems like its not sending my custom fields value.

closed as off-topic by Michael, Nathan Johnson, Nicolai, cjbj, Christine Cooper Feb 20 '18 at 11:37

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

2
+25

There is the Woocommerce Documentation article: Add a surcharge to cart and checkout – uses fees API. Examples are taken from there, no credit whatsoever goes to me.

Example 1: 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
 * Uses the WooCommerce fees API
 *
 * Add to theme functions.php
*/
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, '' );

}

Example 2: Add a standard $ value surcharge to all transactions

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;
}

Example 3: 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
 *
 * Change $percentage to set the surcharge to a value to suit
 *
 * Add countries to array('US'); to include more countries to surcharge
 * http://en.wikipedia.org/wiki/ISO_3166-1#Current_codes for available alpha-2 country codes 
 *
 * Change in_array to !in_array to EXCLUDE the $countries array from surcharges
 *
 * Uses the WooCommerce fees API
 * Add to theme functions.php
 */
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;

}
  • Thanks. But I have to implement interactive handling fee. I know how to add fee but I don't know how to send user input data for packaging type using ajax. Just like selecting different shipping methods. – Shaheed Mon Feb 13 '18 at 17:00
  • @ShaheedMon I have no better suggestion, then you coping, maybe extending, if possible, what the developers of woocommerce did there. – Nicolai Feb 15 '18 at 14:15

Not the answer you're looking for? Browse other questions tagged or ask your own question.