0

Per http://docs.woothemes.com/document/add-a-surcharge-to-cart-and-checkout-uses-fees-api/

    add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge() {
  global $woocommerce;

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

    $county     = array('ME');
    $percentage     = 0.01;

    if ( in_array( $woocommerce->customer->get_shipping_state(), $state ) ) :
        $surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;
        $woocommerce->cart->add_fee( 'Surcharge', $surcharge, true, 'standard' );
    endif;

}

How do I add more than one state? So if the person is in OH or FL - I'd like to add a rate for each state.

  • Once state is selected you reqire Ajax to update cart value as well, refer code https://stackoverflow.com/questions/50390333/on-country-change-ajax-update-checkout-for-shipping-in-woocommerce – noobnicks Aug 08 '20 at 19:47

2 Answers2

1

Write some new code for it...I'll do it for you this time:

add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge() {
    global $woocommerce;

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

    $state = $woocommerce->customer->get_shipping_state();

    if ( $state == 'ME' ) {
        $surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * 0.01;
        $woocommerce->cart->add_fee( 'Surcharge', $surcharge, true, 'standard' );
    } elseif ( $state == 'OH' ) {
        // Do something else
    } elseif ( in_array( $state, array('FL', 'CA') ) ) {
        // Even something else
    }
}
rnevius
  • 26,578
  • 10
  • 58
  • 86
  • Thank you! I failed to say it's a different rate for each state. This will become more complicated? – Chriz Canada Dec 10 '14 at 16:04
  • Is every state included? If so, you're probably better off using [array_key_exists()](http://php.net/manual/en/function.array-key-exists.php), and adding each of the states as `keys` of an array, and rates as their paired `values`. – rnevius Dec 10 '14 at 16:15
  • I don't understand...You won't even need `elseif` in that case. Just one `if` statement – rnevius Dec 10 '14 at 16:20
  • Even if I use every state (51) and each state has a different percentage? – Chriz Canada Dec 10 '14 at 16:50
  • Correct, one `if` statement. Don't write `51` of them, whatever you do. `array_key_exists` is the ticket. – rnevius Dec 10 '14 at 16:54
0
add_action( 'woocommerce_cart_calculate_fees','xa_custom_surcharge' );
function xa_custom_surcharge() {
    global $woocommerce;
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;
    $state      = array('MH');
    $surcharge  = 10;
    if ( in_array( WC()->customer->shipping_state, $state ) ) {
        $woocommerce->cart->add_fee( 'Additional Charge', $surcharge, true, '' );
    }
}

Try this simple snippet. Got from here

mujuonly
  • 11,370
  • 5
  • 45
  • 75