Jump to content

Affiliate Referral Link not Displaying in Custom Email Template


CMS911

Recommended Posts

I created a general email template to send to new affiliate clients. I want their unique affiliate link to display in the email template. I tried using {$affiliate_referral_url} and {$referrallink} merge fields but it always displays blank.

The client I am sending it to is activated as an affiliate. Even more puzzling the client gets the monthly referrals report and it has their link in it and in that email template it uses {$affiliate_referral_url} merge field. 
 

Link to comment
Share on other sites

{$affiliate_referral_url} is only exposed to email templates under the Affiliates template group and not the General group.

You have two options:

1. Put the email template under the Affiliates template group

2. Use a hook to expose {$affiliate_referral_url} in General email templates

<?php

use WHMCS\Mail\Template;
use WHMCS\User\Client\Affiliate;

add_hook('EmailTplMergeFields', 1, static function (array $params) {
    if ($params['type'] !== 'general') {
        return [];
    }

    return ['affiliate_ref_url' => 'Affiliate Referral Url'];
});

add_hook('EmailPreSend', 1, static function (array $params) {
    $templateName = $params['messagename'];
    $check = Template::where('name', $templateName)->where('type', 'general')->count();
    if ($check !== 1) {
        return [];
    }

    $affiliateLink = Affiliate::where('clientid', $params['relid'])->first()?->getReferralLink();

    return ['affiliate_ref_url' => $affiliateLink];
});

Note: First hook adds the information about the variable to the edit template screen 

image.png.ff241456e72156b6ec0743184adad41f.png

Edited by leemahoney3
Link to comment
Share on other sites

21 minutes ago, CMS911 said:

Thank you so much. I wasn't able to add it as an affiliate template it does not give me that option. I like your second suggestion better anyways. Where do I add this code? to which .php?

Ah, didn't notice the option to create an Affiliate related email template wasn't available.

You just need to add it to a PHP file in includes/hooks (e.g. includes/hooks/add_affiliate_link_to_email_templates.php)

Link to comment
Share on other sites

  • 6 months later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated