Jump to content

Hook to cancel invoices for terminated products


Recommended Posts

Great @DennisHermannsen

There shouldn't be that case you've mentioned at first, since only one Late Fee entry would be there on a invoice. Even if there are multiple items, there should only be one Late Fee line so there is no issues with it. 

Anyway, I'll test this out with my test cases and let you know the result as soon as possible.

Did you also include the email notifications to the administrator users upon these actions? If this even possible to implement? If so, including that would be a good function.

Thanks very much for this help, much appreciated 🙂

Link to comment
Share on other sites

  • 5 weeks later...
  • 9 months later...
  • 7 months later...
  • 2 months later...
On 1/13/2022 at 6:23 PM, arowolodaniel said:

can i still use this code? 2022?

FWIW, I tested this today on WHMCS 8.4.1 by ordering a product, provisioning it, leaving the invoice open, and then terminating it. It was of course the simplest of invoices with just the product line item on it, but it worked.

Edited by Troy
Link to comment
Share on other sites

  • 9 months later...

add_hook('InvoiceCancelled', 1, function($vars) {
    // Get the ID of the cancelled invoice
    $invoice_id = $vars['invoiceid'];

    // Get the details of the cancelled invoice
    $invoice = localAPI('GetInvoice', array('invoiceid' => $invoice_id), 'admin');

    // Get the products associated with the cancelled invoice
    $products = $invoice['items']['item'];

    // Loop through each product
    foreach ($products as $product) {
        // If the product is terminated, cancel the invoice
        if ($product['status'] == 'Terminated') {
            localAPI('CancelInvoice', array('invoiceid' => $invoice_id), 'admin');
        }
    }
});
 

Link to comment
Share on other sites

  • 2 months later...

This hook still works perfectly per my testing, You are awesome for providing this to us and I'm super grateful.  I'm curious about Multiple products from the same invoice.  If the invoice contains 5 different products, but only 1 is terminated, it seems to terminate the entire invoice. Is there a way to require the hook to check that ALL the products have been terminated before cancelling? 

Link to comment
Share on other sites

  • 2 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