以编程方式从购物车中删除应用的特定费用 [英] Remove applied specific fee from Cart programmatically
本文介绍了以编程方式从购物车中删除应用的特定费用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已通过以下方式向我的 WooCommerce 购物车收取特定费用:
I have applied a specific fee to my WooCommerce cart in the following way:
WC()->cart->add_fee( __( "Delivery Fee"), 50);
上面代码的作用是,除了小计和运费之外,它还在总和中添加了运费并正确显示了总计.
What the above code does is that in addition to the Subtotal and Shipping charges, it adds the Delivery Fee to the total and shows the grand total correctly.
我现在想以编程方式删除应用的费用,但我无法这样做.
I want to now remove the applied fees programmatically, but I am unable to do so.
我试过了,但它不起作用:
WC()->cart->remove_fees( __( "Delivery Fee"));
这是我的完整代码:
add_action( 'woocommerce_before_cart', 'custom_fees' );
function custom_fees() {
// Add Fees - This WORKS
WC()->cart->add_fee( __( "Delivery Fee"), 50);
// Remove Fees - This DOES NOT WORK
WC()->cart->remove_fees( __( "Delivery Fee"));
}
如何在无需清除购物车的情况下以编程方式移除应用费用?
推荐答案
取决于您的需求,这里有一个解决方案:
Depends on how you need this be, here's one solution:
add_action( 'woocommerce_before_calculate_totals', 'custom_fees' );
function custom_fees() {
// Add Fees - This WORKS
WC()->cart->add_fee( __( "Delivery Fee"), 50); // gets removed
WC()->cart->add_fee( __( "Delivery Fee2"), 150); // will not be removed.
$fees = WC()->cart->get_fees();
foreach ($fees as $key => $fee) {
if($fees[$key]->name === __( "Delivery Fee")) {
unset($fees[$key]);
}
}
WC()->cart->fees_api()->set_fees($fees);
}
这篇关于以编程方式从购物车中删除应用的特定费用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文