解开 remove_non_recurring_fees() WooCommerce 订阅功能

Unhook remove_non_recurring_fees() WooCommerce Subscriptions function

我正在拼命尝试在购物车计算总数时删除一个操作。

这是我的代码:

remove_action('woocommerce_cart_calculate_fees', array('WCS_Cart_Renewal', 'remove_non_recurring_fees'), 1000);

当原始操作挂钩发生在 WooCommerce 订阅插件上时:

// Remove non-recurring fees from renewal carts. Hooked in late (priority 1000), to ensure we handle all fees added by third-parties.
        add_action( 'woocommerce_cart_calculate_fees', array( $this, 'remove_non_recurring_fees' ), 1000 );

不幸的是,我无法删除 remove_non_recurring_fees 钩子函数。

知道为什么吗?

When you Look at WCS_Cart_Renewal Class and remove_non_recurring_fees() function, you will see that this function removes all fees first and re-add only recurring fees, when a subscription is involved. This function is hooked with a priority of 1000.

除了尝试删除触发此函数的操作挂钩,您还有 2 个其他选择:

1).对于您通过主题的 functions.php 文件添加的自定义费用:

您只需使用更高的优先级,如下例所示:

add_action( 'woocommerce_cart_calculate_fees', 'my_custom_fee', 2000 );
function my_custom_fee( $cart ) {
     // Your code
}

2).或者更好地使用 woocommerce_subscriptions_is_recurring_fee 可用的过滤器挂钩:

这个过滤器挂钩允许 re-add 所有需要的费用,这些费用不会通过这个简单的代码行重复出现:

add_filter( 'woocommerce_subscriptions_is_recurring_fee', '__return_true' );

代码进入活动子主题(或活动主题)的 functions.php 文件。已测试并有效。