Reject a referral coupon when the advocate has no active or pending-cancel subscription. WooCommerce Subscriptions is required.
Developer note:
Add custom PHP in a small custom plugin or child theme, and test it on a staging site before production. This example requires ongoing maintenance when extension APIs change.
functions.php
<?php
add_filter( 'automatewoo/referrals/validate_coupon_for_user', 'my_store_validate_referral_subscription', 10, 3 );
add_filter( 'automatewoo/referrals/validate_coupon_for_guest', 'my_store_validate_referral_subscription', 10, 3 );
/**
* Require the advocate to have an active subscription.
*
* @param true|WP_Error $valid Existing validation result.
* @param int|string $customer Customer identifier.
* @param int $advocate_id Advocate user ID.
* @return true|WP_Error
*/
function my_store_validate_referral_subscription( $valid, $customer, $advocate_id ) {
if ( is_wp_error( $valid ) ) {
return $valid;
}
if ( empty( $customer ) || ! function_exists( 'wcs_get_users_subscriptions' ) ) {
return new WP_Error( 'coupon_invalid', __( 'This coupon is invalid.', 'my-store' ) ); // phpcs:ignore WordPress.WP.I18n.TextDomainMismatch
}
foreach ( wcs_get_users_subscriptions( absint( $advocate_id ) ) as $subscription ) {
if ( $subscription->has_status( array( 'active', 'pending-cancel' ) ) ) {
return true;
}
}
return new WP_Error( 'coupon_invalid', __( 'This coupon is invalid.', 'my-store' ) ); // phpcs:ignore WordPress.WP.I18n.TextDomainMismatch
}