Skip to content

Instantly share code, notes, and snippets.

@goranefbl
Created April 5, 2025 06:23
Show Gist options
  • Save goranefbl/d67a546345297cfea5d014450333ce67 to your computer and use it in GitHub Desktop.
Save goranefbl/d67a546345297cfea5d014450333ce67 to your computer and use it in GitHub Desktop.
refer a friend hook
<?php
/**
* Refer a Friend integration with points
*/
class WPGL_Refer_A_Friend
{
private static $instance = null;
public static function init()
{
if (self::$instance === null) {
self::$instance = new self();
}
self::add_hooks();
}
private static function add_hooks()
{
// Add points instead of referral coupon
add_action('gens_before_generate_user_coupon', [__CLASS__, 'award_referral_points'], 10, 3);
// Prevent coupon generation
add_filter('gens_raf_generate_coupon', [__CLASS__, 'prevent_coupon_generation'], 10, 2);
}
/**
* Award points for referral
*/
public static function award_referral_points($user_id, $type, $order)
{
// Get earning actions
$earning_actions = WPGL_Points_Core::get_earning_actions();
// Find referral action
$referral_action = array_filter($earning_actions, function ($action) {
return $action['type'] === WPGL_Points_Source_Type::REFER_FRIEND && $action['enabled'];
});
if (empty($referral_action)) {
return;
}
$action = reset($referral_action);
$points = $action['points'];
// Add points
do_action(
'wpgens_loyalty_update_points',
$user_id,
$points,
WPGL_Points_Activity_Type::EARN,
WPGL_Points_Source_Type::REFER_FRIEND,
$order ? $order->get_id() : null,
'Referral reward'
);
}
/**
* Prevent coupon generation
*/
public static function prevent_coupon_generation($generate, $order_id)
{
// Get earning actions
$earning_actions = WPGL_Points_Core::get_earning_actions();
// Find referral action
$referral_action = array_filter($earning_actions, function ($action) {
return $action['type'] === WPGL_Points_Source_Type::REFER_FRIEND && $action['enabled'];
});
// If there's an enabled referral action, prevent coupon generation
if (!empty($referral_action)) {
return false;
}
// Otherwise, allow coupon generation (return the original value)
return $generate;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment