Php 向Woocommerce添加百分比费用和固定费用
在Woocommerce中,我想在结帐页面上加上3%的手续费和30美分的“固定”费用 我已经使用下面提到的代码添加了处理费:Php 向Woocommerce添加百分比费用和固定费用,php,wordpress,woocommerce,cart,checkout,Php,Wordpress,Woocommerce,Cart,Checkout,在Woocommerce中,我想在结帐页面上加上3%的手续费和30美分的“固定”费用 我已经使用下面提到的代码添加了处理费: add_action( 'woocommerce_cart_calculate_fees', 'woocommerce_custom_surcharge' ); function woocommerce_custom_surcharge() { global $woocommerce; if ( is_admin() && ! defin
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, '' );
如果您不需要单独显示,只需注意“处理费”包括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, '' );
两者都应该起作用。这将取决于您下游需要什么如果您需要以允许单独显示的方式添加这两项附加费,您可以:
$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, '' );
如果您不需要单独显示,只需注意“处理费”包括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文件。测试和工作
使用此钩子时,$global$woocommerce不需要code>,因为钩住的函数可以使用WC\u Cart
对象变量作为参数
您的代码有点过时,请尝试以下将在百分比费用中添加固定费用的方法:
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文件。测试和工作
使用此钩子时,$global$woocommerce不需要code>,因为钩住的函数可以使用WC\u Cart
对象变量作为参数
add\u fee()
函数中有什么?它是否返回值?您是否尝试将.30
添加到您的$附加费
变量值…?我在您的代码中没有看到$费用
。你是在问如何用PHP做数学吗?@MonkeyZeus这会让你在这个网站上感到惊讶:-)…@misorude很遗憾,这并不让我感到惊讶。每次来这里,我都发现参与的理由越来越少:-/code>add_fee()函数中有什么?它是否返回值?您是否尝试将.30
添加到您的$附加费
变量值…?我在您的代码中没有看到$费用
。你是在问如何用PHP做数学吗?@MonkeyZeus这会让你在这个网站上感到惊讶:-)…@misorude很遗憾,这并不让我感到惊讶。每次来这里,我都会发现越来越少的参与的理由:-/你的代码工作得很好!对于100$(仅限)以上的订单,是否可以使用此代码?@UsamaShabbier Add before$percentage=0.03代码>这一行如果($cart->小计谢谢。你真是一个传奇人物❤️ 很抱歉再次打扰您,但我还有最后一个问题:-如何使此代码仅适用于小订单?我只想对小订单(低于100$)申请处理费。您的代码工作得很好!是否可以使此代码适用于高于100$(仅限)的订单?@UsamaShabbier Add before$percentage=0.03;
这一行if($cart->小计谢谢。你真是个传奇人物❤️ 很抱歉再次打扰您,但我还有最后一个问题:-如何使此代码仅适用于小订单?我想仅适用于小订单(低于100美元)的处理费。