Hooking into invoice paid

This topic contains 8 replies, has 2 voices, and was last updated by  TLN 4 years, 11 months ago.

We have moved to a support ticketing system and our forums are now closed.

Open Support Ticket
  • Author
    Posts
  • #3006

    TLN
    Buyer
    Post count: 131

    Hi Guys,

    I’d like to be able to do some stuff when an invoice is paid. Is there an action hook for this? Could you point me in the right direction?

    Thank you very much for any help.

    #3008

    Patrik
    Moderator
    Post count: 1971

    Yes, there are few hooks you can use as per your requirements:

    do_action( 'wpinv_status_' . $new_status, $this->ID, $old_status );
                do_action( 'wpinv_status_' . $old_status . '_to_' . $new_status, $this->ID, $old_status );
                do_action( 'wpinv_update_status', $this->ID, $new_status, $old_status );

    So you can use the following hooks as per your requirements:
    ‘wpinv_status_paid’, ‘wpinv_status_draft_paid’, ‘wpinv_update_status’.

    Regards,
    Patrik

    #3013

    TLN
    Buyer
    Post count: 131

    Thanks Patrik.

    Is it also possible to know the amount of the invoice? In other words, when the invoice is paid, how can we know how much was paid? Thank you very much sir.

    #3014

    Patrik
    Moderator
    Post count: 1971

    Hi,

    You can use the wpinv_get_invoice($post_id) to get the invoice object and then use get_total() method to get the invoice total amount.

    Regards,
    Patrik

    #3015

    TLN
    Buyer
    Post count: 131

    Thank you! This works perfectly.

    #3020

    TLN
    Buyer
    Post count: 131

    Ok, so this mostly works, except that it does not get triggered when an invoice is created for the first time from the front end.

    How could i hook to a pending invoice that was just created for a user and is pending payment?

    I tried this but it does not work for me:

    function invoice_status_update($invoice_id, $new_status, $old_status ) {

    if ($new_status == ‘wpi-pending’ && $old_status != ‘wpi-pending’) {
    // nothing happens when the invoice is created from the front end and set to “Payment Pending”
    }

    }
    add_action( ‘wpinv_update_status’, ‘invoice_status_update’, 10, 3 );

    #3021

    Patrik
    Moderator
    Post count: 1971
    This reply has been marked as private.
    #3022

    TLN
    Buyer
    Post count: 131
    This reply has been marked as private.
    #3023

    TLN
    Buyer
    Post count: 131

    Here’s my code in case any one else needs to know when an invoice is first created. This will fire on creation:

    function on_invoice_created( $post_id, $post, $update ) {

    $author_id=$post->post_author;

    $updated = ($update) ? “updated” : “saved”;

    if ($post->post_type == ‘wpi_invoice’) {

    if ($updated==’saved’) {

    $invoice = wpinv_get_invoice($post_id);
    $invoice_total = $invoice->get_total();
    $invoice_id = $post_id

    // do some stuff here

    }

    }

    add_action( ‘save_post_wpi_invoice’, ‘on_invoice_created’, 10, 3 );

Viewing 9 posts - 1 through 9 (of 9 total)

We have moved to a support ticketing system and our forums are now closed.

Open Support Ticket