在 WooCommerce 中根据折扣后的总额添加计算费用
Add a calculated fee based on total after discounts in WooCommerce
我正在尝试根据应用折扣后的小计向我的 woocommerce 购物车添加费用:
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge() {
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$percentage = 0.01;
$surcharge = $woocommerce->cart->subtotal - $woocommerce->cart->get_cart_discount_total();
$woocommerce->cart->add_fee( 'Surcharge', $surcharge, true, '' );
}
我不相信像 $woocommerce->cart->get_cart_discount_total()
这样的调用可以用在动作挂钩中,这就是为什么我一直收到 0.00
的费用。
我还了解到一些 WC 值已被弃用并且将始终显示为零,但这并不能解释为什么这些金额出现在过滤器中而不是操作中。
我还可以在操作中使用什么来获得相同的号码并向其添加百分比费用?
WC_Cart
对象参数包含在 woocommerce_cart_calculate_fees
操作挂钩中。我还使用百分比金额计算,因为我想你只是在代码中忘记了它。
所以你应该试试这个:
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 );
}
代码进入您活跃的子主题(或主题)的 function.php 文件。
已测试并完美运行。
Note: Also global $woocommerce;
with $woocommerce->cart
has been replaced by WC()->cart
since a long time. The WC()
woocommerce object already include itself global $woocommerce;
…
具体更新:
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 and your state
$percent = 6;
$state = array('MI');
$coupon_total = $cart->get_discount_total();
// FEE calculation
$fee = ( $cart->subtotal - $coupon_total ) * $percent / 100;
if ( $fee > 0 && WC()->customer->get_shipping_state() == $state )
$cart->add_fee( __('Tax', 'woocommerce'), $fee, false);
}
代码进入您活跃的子主题(或主题)的 function.php 文件。
已测试并有效。
我用来创建优惠券的插件挂接到 after_calculate_totals 并随后调整金额。在插件之前触发的任何内容都不计入调整后的总数。我能够使用插件中的变量调用特定金额来创建我需要的费用金额
对于其他感兴趣的人:我正在使用 ignitewoo 礼券专业版插件,并希望根据优惠券后的余额创建费用。这是经过一些修改的 Loic 代码:
add_action( 'woocommerce_cart_calculate_fees','wc_custom_surcharge', 10, 1 );
function wc_custom_surcharge( $cart) {
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$state = array('MI');
// HERE set your percent rate
$percent = 6;
$coupTotal = 0;
foreach ( $woocommerce->cart->applied_coupons as $cc ) {
$coupon = new WC_Coupon( $cc );
$amount = ign_get_coupon_amount( $coupon );
$coupTotal += $amount;
}
// Fee calculation
$fee = ($cart->subtotal - $coupTotal) * $percent/100;
if (( $fee > 0 ) AND (in_array( WC()->customer->shipping_state, $state ) ))
$cart->add_fee( __('Tax', 'woocommerce'), $fee, false);
}
我正在尝试根据应用折扣后的小计向我的 woocommerce 购物车添加费用:
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge() {
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$percentage = 0.01;
$surcharge = $woocommerce->cart->subtotal - $woocommerce->cart->get_cart_discount_total();
$woocommerce->cart->add_fee( 'Surcharge', $surcharge, true, '' );
}
我不相信像 $woocommerce->cart->get_cart_discount_total()
这样的调用可以用在动作挂钩中,这就是为什么我一直收到 0.00
的费用。
我还了解到一些 WC 值已被弃用并且将始终显示为零,但这并不能解释为什么这些金额出现在过滤器中而不是操作中。
我还可以在操作中使用什么来获得相同的号码并向其添加百分比费用?
WC_Cart
对象参数包含在 woocommerce_cart_calculate_fees
操作挂钩中。我还使用百分比金额计算,因为我想你只是在代码中忘记了它。
所以你应该试试这个:
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 );
}
代码进入您活跃的子主题(或主题)的 function.php 文件。
已测试并完美运行。
Note: Also
global $woocommerce;
with$woocommerce->cart
has been replaced byWC()->cart
since a long time. TheWC()
woocommerce object already include itselfglobal $woocommerce;
…
具体更新:
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 and your state
$percent = 6;
$state = array('MI');
$coupon_total = $cart->get_discount_total();
// FEE calculation
$fee = ( $cart->subtotal - $coupon_total ) * $percent / 100;
if ( $fee > 0 && WC()->customer->get_shipping_state() == $state )
$cart->add_fee( __('Tax', 'woocommerce'), $fee, false);
}
代码进入您活跃的子主题(或主题)的 function.php 文件。
已测试并有效。
我用来创建优惠券的插件挂接到 after_calculate_totals 并随后调整金额。在插件之前触发的任何内容都不计入调整后的总数。我能够使用插件中的变量调用特定金额来创建我需要的费用金额
对于其他感兴趣的人:我正在使用 ignitewoo 礼券专业版插件,并希望根据优惠券后的余额创建费用。这是经过一些修改的 Loic 代码:
add_action( 'woocommerce_cart_calculate_fees','wc_custom_surcharge', 10, 1 );
function wc_custom_surcharge( $cart) {
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$state = array('MI');
// HERE set your percent rate
$percent = 6;
$coupTotal = 0;
foreach ( $woocommerce->cart->applied_coupons as $cc ) {
$coupon = new WC_Coupon( $cc );
$amount = ign_get_coupon_amount( $coupon );
$coupTotal += $amount;
}
// Fee calculation
$fee = ($cart->subtotal - $coupTotal) * $percent/100;
if (( $fee > 0 ) AND (in_array( WC()->customer->shipping_state, $state ) ))
$cart->add_fee( __('Tax', 'woocommerce'), $fee, false);
}