Php add_fee()是在结帐总额上添加两次费用
我正在尝试根据用户选择的字段在结帐页面上添加费用 当我使用WC_cartPhp add_fee()是在结帐总额上添加两次费用,php,wordpress,woocommerce,checkout,cart,Php,Wordpress,Woocommerce,Checkout,Cart,我正在尝试根据用户选择的字段在结帐页面上添加费用 当我使用WC_cartadd_fee()方法时,它会在总额中添加两次费用,而在结帐时只显示一次。所以结帐总额的计算是错误的 以下是我正在尝试的代码: add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge',50 ); function woocommerce_custom_surcharge(WC_Cart $cart) { global
add_fee()
方法时,它会在总额中添加两次费用,而在结帐时只显示一次。所以结帐总额的计算是错误的
以下是我正在尝试的代码:
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge',50 );
function woocommerce_custom_surcharge(WC_Cart $cart) {
global $woocommerce;
$eu_array = array('BE','BG','CZ','DK','DE','EE','IE','GR','ES','FR','HR','CY','LV','LT','LU','HU','MT','NL','AT','PT','RO','SI','SK','FI','GB');
if(preg_match("#customer_type=company#", $_POST['post_data']) ){
if(in_array($_POST['s_country'], $eu_array)){
$woocommerce->cart->add_discount(sanitize_text_field( 'cart_company_dis' ));
$ship = $cart->shipping_total;
$shipp = $ship - ($ship*100/122);
wc()->cart->add_fee('Exclude shipping VAT', -$shipp);
}
}elseif(preg_match("#customer_type=customer#", $_POST['post_data']) ){
$woocommerce->cart->remove_coupon(sanitize_text_field( 'cart_company_dis' ));
}
}
该代码在结帐页面上应用折扣,并根据客户类型和国家/地区添加费用
折扣正确地减少了,但费用增加了两次
我怎样才能避免这个问题
谢谢。您可以使用WC_购物车方法检查购物车中是否已经应用了特定费用,以避免重复费用问题 因此,您的代码将有一些更改:
// NOTE:
// No need of "global woocommerce" and "woocommerce->cart" (old syntax) replaced by "WC()->cart"
// Here "$cart_obj" replace "WC()->cart" everywhere, as it included as argument in the function…
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge',50 );
function woocommerce_custom_surcharge( $cart_obj ) {
$eu_array = array('BE','BG','CZ','DK','DE','EE','IE','GR','ES','FR','HR',
'CY','LV','LT','LU','HU','MT','NL','AT','PT','RO','SI','SK','FI','GB');
if(preg_match("#customer_type=company#", $_POST['post_data']) ){
if(in_array($_POST['s_country'], $eu_array)){
$cart_obj->add_discount(sanitize_text_field( 'cart_company_dis' ));
$ship = $cart_obj->shipping_total;
$shipp = $ship - ($ship * 100 / 122);
// Getting the cart fees
$cart_fees = $cart_obj->get_fees();
$has_the_fee = true;
// Iterating through each cart fees
foreach($cart_fees as $fee_obj)
if( 'exclude-shipping-vat' != $fee_obj->id ){}
$has_the_fee = false; // Has not 'Exclude shipping VAT' fee
// Add the fee if it doesn't exist yet (avoiding duplicate fee)
if( empty($cart_fees) || $has_the_fee )
$cart_obj->add_fee( __( 'Exclude shipping VAT', 'woocommerce' ), -$shipp );
}
} elseif( preg_match( "#customer_type=customer#", $_POST['post_data'] ) ){
$cart_obj->remove_coupon( sanitize_text_field( 'cart_company_dis' ) );
}
}
该代码位于活动子主题(或主题)的function.php文件或任何插件文件中
此代码已经过测试并运行。感谢@LoicTheAztec提供的有用答案。不过,我认为可能有几个输入错误——要么是这样,要么是我误解了:1)
if('exclude shipping vat'!=$fee\u obj->id){}
——似乎在$的值更改之前关闭了if语句。2) 测试if(empty($cart\u fees)| |$has\u fees)
是否应该改为if(empty($cart\u fees)|!$has\u fees)
(added!for not),因为如果购物车已经“拥有”了费用,就不需要增加费用?(请注意,很抱歉,我不知道如何在评论中添加换行符。关于行末尾的两个空格的提示似乎不起作用。可能我只是遗漏了一些明显的内容--)