尝试在 WooCommerce 结帐中添加 5% 乘以小计的佣金。但是,我得到的只是一个空白页面,并且该网站停止工作。
这是代码:
add_action( 'woocommerce_cart_calculate_fees', 'add_commission', 10, 1 );
function add_commission( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
// global, is this needed?
global $woocommerce;
// get the cart subtotal
$subtotal = WC()->cart->get_cart_subtotal();
// commission fee of 5%
$commission_fee = 0.05;
// the total commission is the 5% multiplied by the subtotal
$total_commission = $commission_fee * $subtotal;
// apply the commission
$cart->add_fee( 'Commission', $total_commission, true );
}
主要错误是
get_cart_subtotal()
方法,即您尝试与浮点数相乘的格式化购物车小计 HTML(字符串)。相反,您应该使用 get_subtotal() 方法。
所以正确的代码是:
add_action( 'woocommerce_cart_calculate_fees', 'add_commission' );
function add_commission( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// get the cart subtotal
$subtotal = $cart->get_subtotal();
// commission fee of 5%
$commission_fee = 0.05;
// the total commission is the 5% multiplied by the subtotal
$total_commission = $commission_fee * $subtotal;
// apply the commission
$cart->add_fee( 'Commission', $total_commission, true );
}