我们在使用 WooCommerce 建站的时候有些特殊情况下,网店需向顾客收取一部分额外的费用以摊薄成本。比较典型的应用场景就是开发票费用、出关的关税等等。而 WooCommerce 默认无法在订单中添加这类额外费用,只能添加税费等附加费用。但是,如果把这部分额外费用直接加m S J % [ O到商品价格上就不妥了,会造成商品价格更高、无竞争力。因此,我们有必要为y h m t f [ v WooCommerce 订单添加一项或多项附加费用来实* = d Z ^ Y @ *现此功能。以下tag/23/" target="_blank">代码以收取每单商品价格加运费总和的 5` O o%作为附加费用为例来说明如何添加T : Q附加费用功能:
一、为所有交易添加基于百分比的附加费
- /**
- * 添加1%的附加费到你的购物车和结算页面
- *f n a f : G y y 更改百分比以将附加费设置为适合的值
- */
- add_action( 'wooE % = ) G g \commerce_cart_calW c vculate_fees','woocommerce_custom_surcha5 i ) J ! hrge' );
- fL M 1 C 1unction woocommerce_custon \ !m_surcharge() {
- global $woocommerce;
- if ( is_a6 [ J d \ + D 8dmin() &a_ p | w j Omp;& ! defined( 'DOw , | . { dING_AJAX' ) )
- return;
- $percenG R ( ~tage = 0.01V V G p _ ~;
- $surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;
- $woocommerce->cart->add_fee( 'Surcharge', $sur) t B q 9 c \charge, trug S ~ & Je, '' );
- }
二、为所有交易添加固定的附加费
- /**
- * 为购物车/结帐中的所有交易添加固定f 5 C u ^ F的附加费
- */
- add_action(. 6 : \ f % K 'woocommerce_cart_calculate_fees','wc_add_surch\ 7 M A S Karge' U s ? t U1;;
- functi= U E son wc_add_surcharge() {
- global $woocommerce;
- if ( is_admi! * 8 1nL ( c X L I ( P040;) && ! defined( 'DOING_AJ` 1 D 0 ?AX' ) )
- return;
- $county = array('US');
- // change the $fee to set the surcharge to a value to suit
- $fee = 1.00;
- if ( in} A S_arraL ? ? ;y( WC()->customer->get_shipping_country(&? 4 h L w h K#041;, $county ) ) :
- $woocomm2 / $ =erce->cart->add_fee( 'Surcharge', $fee, true, 'standard' );
- endif;
- }
三、根据交货国家/地区添加附加费
- /**
- * 根据送货国向您的购物车/结算添加1%的附加费
- * 税金、运费及订单小计均包含在附加费内
- */
- add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
- function woocommerce_custom_surd % T | y E f wcharge() &B - O D W E#123z l E - { P Q 1;
- global $woocommerce;
- ifA | } k D o ( is_admin() && ! defined( 'DOING_AJAX' ) )U B 0;
- return;
- $county = array('US')@ C l 2;;
- $percentage = 0.01;
- if ( in_- P U 9 ^ n o { }array( $woocL . k b F ( .ommerce->customer->get_shipping_country(), $county ) ) :
- $surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;
- $woocommerce->cart->add_fee( 'Surcharge', $surcharge, true, '' );
- endif;
- }