向 Woocommerce 添加固定费用的百分比费用
Add a percentage Fee with a fixed Fee to Woocommerce
在 Woocommerce 中,我想在结帐页面添加 3% 的手续费和 30 美分的“固定”费用。
我已经使用下面提到的代码添加了加工费:
add_action( 'woocommerce_cart_calculate_fees', 'woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge() {
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$percentage = 0.03;
$surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;
$woocommerce->cart->add_fee( 'Processing Fee', $surcharge, true, '' );
}
现在,我只需要加上 30 美分的“固定”费用。我怎样才能做到这一点?
我试过了,添加:
$fee = 0.30;
但是,它对我不起作用。
如果您需要以允许单独显示的方式添加两种附加费,您可以:
$percentage = 0.03;
$fixed_fee = 0.30;
$surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;
$woocommerce->cart->add_fee( 'Processing Fee', $surcharge, true, '' );
$woocommerce->cart->add_fee( 'Fixed fee', $fixed_fee, true, '' );
如果您不需要单独展示它们,只需注意 "processing fee" 包括 3% 的附加费和固定的 30 美分:
$percentage = 0.03;
$fixed_fee = 0.30;
$surcharge = (( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage) + $fixed_fee);
$woocommerce->cart->add_fee( 'Processing Fee', $surcharge, true, '' );
两者都应该有效。这将取决于您对下游的需求
您的代码有点过时,请尝试以下操作,这会在百分比费用中增加固定费用:
add_action( 'woocommerce_cart_calculate_fees', 'woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$percentage = 0.03;
$fixed_fee = 0.3;
$percentage_fee = ( $cart->cart_contents_total + $cart->shipping_total ) * $percentage;
$surcharge = $fixed_fee + $percentage_fee;
$cart->add_fee( 'Processing Fee', $surcharge, true );
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
When using this hook outdated $global $woocommerce;
is not needed as the hooked function can use the WC_Cart
object variable as an argument…
在 Woocommerce 中,我想在结帐页面添加 3% 的手续费和 30 美分的“固定”费用。
我已经使用下面提到的代码添加了加工费:
add_action( 'woocommerce_cart_calculate_fees', 'woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge() {
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$percentage = 0.03;
$surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;
$woocommerce->cart->add_fee( 'Processing Fee', $surcharge, true, '' );
}
现在,我只需要加上 30 美分的“固定”费用。我怎样才能做到这一点?
我试过了,添加:
$fee = 0.30;
但是,它对我不起作用。
如果您需要以允许单独显示的方式添加两种附加费,您可以:
$percentage = 0.03;
$fixed_fee = 0.30;
$surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;
$woocommerce->cart->add_fee( 'Processing Fee', $surcharge, true, '' );
$woocommerce->cart->add_fee( 'Fixed fee', $fixed_fee, true, '' );
如果您不需要单独展示它们,只需注意 "processing fee" 包括 3% 的附加费和固定的 30 美分:
$percentage = 0.03;
$fixed_fee = 0.30;
$surcharge = (( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage) + $fixed_fee);
$woocommerce->cart->add_fee( 'Processing Fee', $surcharge, true, '' );
两者都应该有效。这将取决于您对下游的需求
您的代码有点过时,请尝试以下操作,这会在百分比费用中增加固定费用:
add_action( 'woocommerce_cart_calculate_fees', 'woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$percentage = 0.03;
$fixed_fee = 0.3;
$percentage_fee = ( $cart->cart_contents_total + $cart->shipping_total ) * $percentage;
$surcharge = $fixed_fee + $percentage_fee;
$cart->add_fee( 'Processing Fee', $surcharge, true );
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
When using this hook outdated
$global $woocommerce;
is not needed as the hooked function can use theWC_Cart
object variable as an argument…