Php WooCommerce,将折扣券也应用于费用

Php WooCommerce,将折扣券也应用于费用,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我们使用$WooCommerce->cart->add_fee()方法向WooCommerce购物车添加了费用 在使用折扣折扣的商品电子优惠券代码时,在计算折扣时不考虑费用金额。 例如: 售价:15 附加费:5 折扣:10%=>1.5(15的10%) 总数:21.5 但我们也需要它 Product Price: 15 Additional Fee: 5 Discount: 10%=> 2 ( 10% of 15+5) Total: 22 售价:15 附加费:5 折扣:10%=>2(15+5的10

我们使用
$WooCommerce->cart->add_fee()
方法向WooCommerce购物车添加了费用

在使用折扣折扣的商品电子优惠券代码时,在计算折扣时不考虑费用金额。 例如: 售价:15 附加费:5 折扣:10%=>1.5(15的10%) 总数:21.5 但我们也需要它

Product Price: 15 Additional Fee: 5 Discount: 10%=> 2 ( 10% of 15+5) Total: 22 售价:15 附加费:5 折扣:10%=>2(15+5的10%) 总数:22
找不到任何挂钩,因此无法将费用包括在折扣计算过程中

您需要挂钩计算费用并进行相应修改

add_action( 'woocommerce_cart_calculate_fees','wc_custom_surcharge', 10, 1 );
function wc_custom_surcharge( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // HERE set your percent rate
    $percent = 1; // 1%

    // Fee calculation
    $fee = ( $cart->subtotal - $cart->get_cart_discount_total() ) * $percent / 100;

    // Add the fee if it is bigger than O
    if( $fee > 0 )
        $cart->add_fee( __('Surcharge', 'woocommerce'), $fee, true );
}
add_action( 'woocommerce_cart_calculate_fees','wc_custom_surcharge', 10, 1 );
function wc_custom_surcharge( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // HERE set your percent rate
    $percent = 1; // 1%

    // Fee calculation
    $fee = ( $cart->subtotal - $cart->get_cart_discount_total() ) * $percent / 100;

    // Add the fee if it is bigger than O
    if( $fee > 0 )
        $cart->add_fee( __('Surcharge', 'woocommerce'), $fee, true );
}