如何在WooCommerce中为含运费的总金额添加3.5%比例额外费用
WooCommerce包含运费的管理费加收解决方案
你当前使用的代码已预留包含运费的计算逻辑,只需调整两行代码即可实现需求:
- 注释掉仅按不含运费的商品总金额计算管理费的代码行
- 取消预留的「商品总金额+运费」计算逻辑行的注释
修改后的完整代码如下:
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_donation' ); function woocommerce_custom_donation() { global $woocommerce; if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return; $percentage = 0.035; // 3.5%管理费率 // 包含运费的总金额计算管理费 $donation = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage; // 仅按商品总金额计算管理费(已弃用) // $donation = $woocommerce->cart->cart_contents_total * $percentage; $woocommerce->cart->add_fee( '管理费', $donation, true, '' ); }
相关参数说明
$woocommerce->cart->cart_contents_total:购物车中所有商品的总金额(不含运费)$woocommerce->cart->shipping_total:购物车运费总金额add_fee第三个参数为true时,管理费会同步计税,不需要计税可修改为false
内容的提问来源于stack exchange,提问作者cmadurawala
相关产品推荐
相关产品推荐

