Skip to content

Instantly share code, notes, and snippets.

@contemplate
Created December 18, 2022 22:12
Show Gist options
  • Save contemplate/9b1762d1bb1aca28d4d7390f831844aa to your computer and use it in GitHub Desktop.
Save contemplate/9b1762d1bb1aca28d4d7390f831844aa to your computer and use it in GitHub Desktop.
AffiliateWP: Block WC Coupon if not referred - enhanced
add_action( 'woocommerce_coupon_options', 'affwp_only_referral_coupon_option' );
add_action( 'woocommerce_coupon_options_save', 'affwp_store_only_referral_coupon_option' );
add_filter( 'woocommerce_coupon_is_valid', 'affwp_custom_block_coupon_if_no_referrer', 10, 2 );
/**
* Add coupon option to enable only referral validity
*/
function affwp_only_referral_coupon_option() {
global $post;
if ( function_exists( 'affiliate_wp' ) ) {
$affwp_only_referral = get_post_meta( $post->ID, 'affwp_discount_only_referral', true );
?>
<p class="form-field affwp-woo-coupon-field-was-referred">
<label for="only_referral"><?php _e( 'Only for Referrals?', 'affiliate-wp' ); ?></label>
<?php
echo wc_help_tip( __( 'If you would like to have this coupon only valid for referrals.', 'affiliate-wp' ) );
?>
<select style="" id="only_referral" name="only_referral" class="select short">
<option value="">No, anyone can use this coupon</option>
<option value="specific" <?php echo ($affwp_only_referral == 'specific' ? 'selected="selected"' : ''); ?>>Yes, Specific Affiliate Referrals</option>
<option value="any" <?php echo ($affwp_only_referral == 'any' ? 'selected="selected"' : ''); ?>>Yes, Any Affiliate Referrals</option>
</select>
</p>
<?php
}
}
/**
* Store coupon option to enable only referral validity
*/
function affwp_store_only_referral_coupon_option( $coupon_id = 0 ) {
if( empty( $_POST['only_referral'] ) ) {
delete_post_meta( $coupon_id, 'affwp_discount_only_referral' );
return;
}
update_post_meta( $coupon_id, 'affwp_discount_only_referral', $_POST['only_referral'] );
}
/**
* Make coupon invalid if customer is not currently tracking an affiliate (can be any affiliate)
*/
function affwp_custom_block_coupon_if_no_referrer( $return, $coupon ) {
// make sure AffiliateWP is installed
if ( ! function_exists( 'affiliate_wp' ) ) {
return $return;
}
// check to see if the coupon is requires refferal
$is_only_referral_coupon = get_post_meta( $coupon->id, 'affwp_discount_only_referral', true );
// check to see if the coupon is linked to an affiliate
$is_tracked_coupon = get_post_meta( $coupon->id, 'affwp_discount_affiliate', true );
// make sure an affiliate is being tracked
if ( ! affiliate_wp()->tracking->was_referred() && $is_tracked_coupon && ( $is_only_referral_coupon == 'specific') ) {
$return = false; // disallow the coupon
} elseif ( ! affiliate_wp()->tracking->was_referred() && ( $is_only_referral_coupon == 'any') ) {
$return = false; // disallow the coupon
}
return $return;
}
@contemplate
Copy link
Author

Adds a new field to the Coupon add / edit screen to select if this coupon if only for referral orders.
Can be for the specific Affiliate linked to the coupon or any affiliate.

Based on this gist:
https://github.com/awesomemotive/affiliatewp-code-snippet-library/blob/master/misc/block-coupon-if-not-referred.php

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment