/** * Check is a subscription coupon is valid before applying * * @since 1.2 */ public static function validate_subscription_coupon($valid, $coupon) { self::$coupon_error = ''; // ignore non-subscription coupons if (!in_array($coupon->type, array('recurring_fee', 'sign_up_fee', 'recurring_percent', 'sign_up_fee_percent'))) { // but make sure there is actually something for the coupon to be applied to (i.e. not a free trial) if (WC_Subscriptions_Cart::cart_contains_free_trial() && 0 == WC_Subscriptions_Cart::get_cart_subscription_sign_up_fee()) { self::$coupon_error = __('Sorry, this coupon is only valid for an initial payment and the subscription already has a free trial.', 'woocommerce-subscriptions'); } } else { // prevent subscription coupons from being applied to renewal payments if (WC_Subscriptions_Cart::cart_contains_subscription_renewal()) { self::$coupon_error = __('Sorry, this coupon is only valid for new subscriptions.', 'woocommerce-subscriptions'); } // prevent subscription coupons from being applied to non-subscription products if (!WC_Subscriptions_Cart::cart_contains_subscription_renewal() && !WC_Subscriptions_Cart::cart_contains_subscription()) { self::$coupon_error = __('Sorry, this coupon is only valid for subscription products.', 'woocommerce-subscriptions'); } // prevent sign up fee coupons from being applied to subscriptions without a sign up fee if (0 == WC_Subscriptions_Cart::get_cart_subscription_sign_up_fee() && in_array($coupon->type, array('sign_up_fee', 'sign_up_fee_percent'))) { self::$coupon_error = __('Sorry, this coupon is only valid for subscription products with a sign-up fee.', 'woocommerce-subscriptions'); } } if (!empty(self::$coupon_error)) { $valid = false; add_filter('woocommerce_coupon_error', __CLASS__ . '::add_coupon_error', 10); } return $valid; }
/** * Check is a subscription coupon is valid before applying * * @since 1.2 */ public static function validate_subscription_coupon($valid, $coupon) { self::$coupon_error = ''; // ignore non-subscription coupons if (!in_array($coupon->type, array('recurring_fee', 'sign_up_fee', 'recurring_percent', 'sign_up_fee_percent'))) { // make sure there are no other products in the cart which the coupon could be applied to - WC()->cart->get_cart_contents_count() returns the quantity of items in the cart, not the total number of unique items, we need to use WC()->cart->cart_contents for that. if (1 == count(WC()->cart->cart_contents) && 0 == WC_Subscriptions_Cart::get_cart_subscription_sign_up_fee()) { $error_message = __('Sorry, this coupon is only valid for an initial payment and the subscription does not have an initial payment.', 'woocommerce-subscriptions'); // now make sure there is actually something for the coupon to be applied to (i.e. not a free trial or sync'd subscription without any prorated initial amount) if (WC_Subscriptions_Cart::cart_contains_free_trial()) { self::$coupon_error = $error_message; } elseif (WC_Subscriptions_Synchroniser::cart_contains_synced_subscription() && !WC_Subscriptions_Synchroniser::cart_contains_prorated_subscription()) { self::$coupon_error = $error_message; } } } else { // prevent subscription coupons from being applied to renewal payments if (WC_Subscriptions_Cart::cart_contains_subscription_renewal('child')) { self::$coupon_error = __('Sorry, this coupon is only valid for new subscriptions.', 'woocommerce-subscriptions'); } // prevent subscription coupons from being applied to non-subscription products if (!WC_Subscriptions_Cart::cart_contains_subscription_renewal() && !WC_Subscriptions_Cart::cart_contains_subscription()) { self::$coupon_error = __('Sorry, this coupon is only valid for subscription products.', 'woocommerce-subscriptions'); } // prevent sign up fee coupons from being applied to subscriptions without a sign up fee if (0 == WC_Subscriptions_Cart::get_cart_subscription_sign_up_fee() && in_array($coupon->type, array('sign_up_fee', 'sign_up_fee_percent'))) { self::$coupon_error = __('Sorry, this coupon is only valid for subscription products with a sign-up fee.', 'woocommerce-subscriptions'); } } if (!empty(self::$coupon_error)) { $valid = false; add_filter('woocommerce_coupon_error', __CLASS__ . '::add_coupon_error', 10); } return $valid; }
/** * Check if a subscription coupon is valid before applying * * @since 1.2 */ public static function validate_subscription_coupon($valid, $coupon) { if (!apply_filters('woocommerce_subscriptions_validate_coupon_type', true, $coupon, $valid)) { return $valid; } self::$coupon_error = ''; // ignore non-subscription coupons if (!in_array($coupon->type, array('recurring_fee', 'sign_up_fee', 'recurring_percent', 'sign_up_fee_percent', 'renewal_fee', 'renewal_percent', 'renewal_cart'))) { // but make sure there is actually something for the coupon to be applied to (i.e. not a free trial) if ((wcs_cart_contains_renewal() || WC_Subscriptions_Cart::cart_contains_subscription()) && 0 == WC()->cart->subtotal) { self::$coupon_error = __('Sorry, this coupon is only valid for an initial payment and the cart does not require an initial payment.', 'woocommerce-subscriptions'); } } else { // prevent subscription coupons from being applied to renewal payments if (wcs_cart_contains_renewal() && !in_array($coupon->type, array('renewal_fee', 'renewal_percent', 'renewal_cart'))) { self::$coupon_error = __('Sorry, this coupon is only valid for new subscriptions.', 'woocommerce-subscriptions'); } // prevent subscription coupons from being applied to non-subscription products if (!wcs_cart_contains_renewal() && !WC_Subscriptions_Cart::cart_contains_subscription()) { self::$coupon_error = __('Sorry, this coupon is only valid for subscription products.', 'woocommerce-subscriptions'); } // prevent subscription renewal coupons from being applied to non renewal payments if (!wcs_cart_contains_renewal() && in_array($coupon->type, array('renewal_fee', 'renewal_percent', 'renewal_cart'))) { // translators: 1$: coupon code that is being removed self::$coupon_error = sprintf(__('Sorry, the "%1$s" coupon is only valid for renewals.', 'woocommerce-subscriptions'), $coupon->code); } // prevent sign up fee coupons from being applied to subscriptions without a sign up fee if (0 == WC_Subscriptions_Cart::get_cart_subscription_sign_up_fee() && in_array($coupon->type, array('sign_up_fee', 'sign_up_fee_percent'))) { self::$coupon_error = __('Sorry, this coupon is only valid for subscription products with a sign-up fee.', 'woocommerce-subscriptions'); } } if (!empty(self::$coupon_error)) { $valid = false; add_filter('woocommerce_coupon_error', __CLASS__ . '::add_coupon_error', 10); } return $valid; }
/** * Check is a subscription coupon is valid before applying * * * @since 1.2 */ public static function validate_subscription_coupon($valid, $coupon) { // only check subscription coupons if (!in_array($coupon->type, array('recurring_fee', 'sign_up_fee'))) { return $valid; } // prevent subscription coupons from being applied to non-subscription products if (!WC_Subscriptions_Cart::cart_contains_subscription()) { $valid = false; self::$coupon_error = __('This coupon is only valid for subscriptions.', WC_Subscriptions::$text_domain); add_filter('woocommerce_coupon_error', __CLASS__ . '::add_coupon_error', 10); } // prevent sign up fee coupons from being applied to subscriptions without a sign up fee if (0 == WC_Subscriptions_Cart::get_cart_subscription_sign_up_fee() && 'sign_up_fee' == $coupon->type) { $valid = false; self::$coupon_error = __('This coupon is only valid for subscriptions with a sign-up fee.', WC_Subscriptions::$text_domain); add_filter('woocommerce_coupon_error', __CLASS__ . '::add_coupon_error', 10); } return $valid; }