Last active
September 14, 2022 11:29
-
-
Save danielbitzer/4cc95161eb15930f427168fa1382ea12 to your computer and use it in GitHub Desktop.
[Refer A Friend] Custom validation for referral coupons - Restrict coupon use to advocates with active subscriptions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( 'automatewoo/referrals/validate_coupon_for_user', 'my_automatewoo_referral_coupon_validate', 10, 3 ); | |
add_filter( 'automatewoo/referrals/validate_coupon_for_guest', 'my_automatewoo_referral_coupon_validate', 10, 3 ); | |
/** | |
* $valid is true if coupon is valid for user or an instance of WP_Error if the coupon is invalid | |
* | |
* @param WP_Error|true $valid | |
* @param int|string $customer | |
* @param int $advocate_id | |
* @return WP_Error|true | |
*/ | |
function my_automatewoo_referral_coupon_validate( $valid, $customer, $advocate_id ) { | |
if ( is_wp_error( $valid ) ) { | |
// coupon is already invalid so return the validation error | |
return $valid; | |
} | |
/** @var WC_Subscription[] $subscriptions */ | |
$subscriptions = wcs_get_users_subscriptions( $advocate_id ); | |
$statuses = []; | |
foreach ( $subscriptions as $subscription ) { | |
$statuses[] = $subscription->get_status(); | |
} | |
if ( count( array_intersect( $statuses, [ 'active', 'pending-cancel' ] ) ) === 0 ) { | |
return new WP_Error( 'coupon-invalid', __( 'This coupon is invalid.', 'my-theme' ) ); | |
} | |
return true; // allow coupon use | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment