每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;
}
如何添加多个州?所以如果这个人在OH或FL中 - 我想为每个州增加一个费率。
答案 0 :(得分:1)
为它写一些新代码......这次我会为你做这件事:
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
}
}
答案 1 :(得分: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, '' );
}
}
试试这个简单的代码段。来自here