根据小计向 WooCommerce Checkout 添加佣金

问题描述 投票:0回答:1

尝试在 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 );
}
wordpress woocommerce checkout fee
1个回答
0
投票

主要错误是

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 );
}
© www.soinside.com 2019 - 2024. All rights reserved.