Jump to content

I want to make hook that abort all email unless client have a paid invoice in his account.


MAqib1087

Recommended Posts

Hello @MAqib1087

You can try with below code it should work 

add_hook('EmailPreSend', 1, function($vars) {
    // Get the client ID from the email variables
    $clientId = $vars['clientid'];

    // Check if the client has any paid invoices
    $paidInvoices = \App\Models\Invoice::where('client_id', $clientId)
        ->where('status', 'Paid')
        ->count();

    // If the client has no paid invoices, abort the email
    if ($paidInvoices == 0) {
        return 'abort';
    }
});

 

Link to comment
Share on other sites

5 hours ago, markwilly said:

You can set up a system that checks whether a client has a paid invoice before sending any emails. If the client doesn't have a paid invoice, the email won't be sent. This ensures that only clients with active, paid invoices will receive communication

  I want someone to do it for me to make sure it works 100%. 

On 12/28/2024 at 12:38 PM, WHMCS Solution said:

Hello @MAqib1087

You can try with below code it should work 

add_hook('EmailPreSend', 1, function($vars) {
    // Get the client ID from the email variables
    $clientId = $vars['clientid'];

    // Check if the client has any paid invoices
    $paidInvoices = \App\Models\Invoice::where('client_id', $clientId)
        ->where('status', 'Paid')
        ->count();

    // If the client has no paid invoices, abort the email
    if ($paidInvoices == 0) {
        return 'abort';
    }
});

 

I have tried many codes but does not work. 

 

 

Anyone? 

Link to comment
Share on other sites

On 12/28/2024 at 12:38 PM, WHMCS Solution said:

Hello @MAqib1087

You can try with below code it should work 

add_hook('EmailPreSend', 1, function($vars) {
    // Get the client ID from the email variables
    $clientId = $vars['clientid'];

    // Check if the client has any paid invoices
    $paidInvoices = \App\Models\Invoice::where('client_id', $clientId)
        ->where('status', 'Paid')
        ->count();

    // If the client has no paid invoices, abort the email
    if ($paidInvoices == 0) {
        return 'abort';
    }
});

 

we got this error after your code

Error: Class 'App\Models\Invoice' not found in

Link to comment
Share on other sites

@MAqib1087

try below code

use WHMCS\Database\Capsule; 

add_hook('EmailPreSend', 1, function($vars) {
    // Get the client ID from the email variables
    $clientId = $vars['clientid'];

    // Check if the client has any paid invoices
    $paidInvoices = Capsule::table('tblinvoices')
        ->where('client_id', $clientId)
        ->where('status', 'Paid')
        ->count();

    // If the client has no paid invoices, abort the email
    if ($paidInvoices == 0) {
        return 'abort';
    }
});

Link to comment
Share on other sites

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