我已经找到了一种方法,但欢迎有更好的方法。
如OP注释中所述,函数remove\\u coups()位于文件woocommerce Subscriptions/includes/class WC Subscriptions-Coupon中的class WC\\u Subscriptions\\u-Coupon中。php正在删除不是“Recurtive\\u fee”或“Recurtive\\u percent”的优惠券。没有添加我们可以点击的过滤器来分配我们自己的定制优惠券类型,因此似乎没有任何机会超越这一点。
为了解决这个问题,我们可以使用与remove\\u tuops()方法相同的代码创建自己的过滤器,在本例中称为remove\\u tuops\\u custom(),然后以与原始类中相同的方式添加此过滤器。
但原始方法仍在运行,并将删除我们的优惠券,因此我们需要通过添加remove\\u操作(“woocommerce\\u before\\u calculate\\u totals”,“WC\\u Subscriptions\\u优惠券::remove\\u优惠券”,10)从运行中删除原始筛选器;在新功能的顶部。
我不喜欢这种方法,以防原始代码的更改程度会中断或停止我们的工作,但这是目前我能看到的唯一方法,除非修改原始代码以添加我们可以使用的过滤器来添加我们自己的自定义类型。
function remove_coupons_custom( $cart )
{
// Remove original action added by WC_Subscriptions_Coupon class
remove_action( \'woocommerce_before_calculate_totals\', \'WC_Subscriptions_Coupon::remove_coupons\', 10 );
// Create WC_Subscriptions_Coupon instance so we can replace self reference below in original method
$WC_Subscriptions_Coupon = new WC_Subscriptions_Coupon;
$calculation_type = WC_Subscriptions_Cart::get_calculation_type();
// Only hook when totals are being calculated completely (on cart & checkout pages)
if ( \'none\' == $calculation_type || ! WC_Subscriptions_Cart::cart_contains_subscription() || ( ! is_checkout() && ! is_cart() && ! defined( \'WOOCOMMERCE_CHECKOUT\' ) && ! defined( \'WOOCOMMERCE_CART\' ) ) ) {
return;
}
$applied_coupons = $cart->get_applied_coupons();
// If we\'re calculating a sign-up fee or recurring fee only amount, remove irrelevant coupons
if ( ! empty( $applied_coupons ) ) {
// Keep track of which coupons, if any, need to be reapplied immediately
$coupons_to_reapply = array();
foreach ( $applied_coupons as $coupon_code ) {
$coupon = new WC_Coupon( $coupon_code );
$coupon_type = wcs_get_coupon_property( $coupon, \'discount_type\' );
if ( in_array( $coupon_type, array( \'recurring_fee\', \'recurring_percent\', \'sign_up_and_recurring_percent\' ) ) ) { // always apply coupons to their specific calculation case
if ( \'recurring_total\' == $calculation_type ) {
$coupons_to_reapply[] = $coupon_code;
} elseif ( \'none\' == $calculation_type && ! WC_Subscriptions_Cart::all_cart_items_have_free_trial() ) { // sometimes apply recurring coupons to initial total
$coupons_to_reapply[] = $coupon_code;
} else {
$WC_Subscriptions_Coupon::$removed_coupons[] = $coupon_code;
}
} elseif ( ( \'none\' == $calculation_type ) && ! in_array( $coupon_type, array( \'recurring_fee\', \'recurring_percent\' ) ) ) { // apply all coupons to the first payment
$coupons_to_reapply[] = $coupon_code;
} else {
$WC_Subscriptions_Coupon::$removed_coupons[] = $coupon_code;
}
}
// Now remove all coupons (WC only provides a function to remove all coupons)
$cart->remove_coupons();
// And re-apply those which relate to this calculation
$cart->applied_coupons = $coupons_to_reapply;
if ( isset( $cart->coupons ) ) { // WC 2.3+
$cart->coupons = $cart->get_coupons();
}
}
}
add_action( \'woocommerce_before_calculate_totals\', \'remove_coupons_custom\', 10 );