在 Woocommerce 中,我想在总计上增加 3.5%(包括运费)。现在我正在使用这些代码,但这只计算不含运费。
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%
// uncomment the line below to include shipping fees in the donation/surcharge calculation
// $donation = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;
$donation = $woocommerce->cart->cart_contents_total * $percentage;
$woocommerce->cart->add_fee( 'Administration Fees', $donation, true, '' );
}
最佳答案
你也可以试试这个。
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_donation',10, 1 );
function woocommerce_custom_donation() {
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$percentage = 3.5;
$cart_total = $woocommerce->cart->cart_contents_total;
$shipping_total = $woocommerce->cart->get_shipping_total() ?: 0;
$tax_total = $woocommerce->cart->get_taxes_total() ?: 0;
$grand_total = $cart_total + $shipping_total + $tax_total;
$donation = $grand_total * $percentage / 100;
$woocommerce->cart->add_fee( 'Administration Fees', $donation, true, '' );
}
关于php - 在 woocommerce 中添加总百分比(包括运费),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69051889/