• Resolved lawkwok

    (@lawkwok)


    Is there a way to create invoice numbers upon order receipt instead of manually when the PDF button is pressed? I know you can automatically attach invoices to the Admin new order email, but I’d rather do this programatically and not have so many attachments in my email.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Contributor Ewout

    (@pomegranate)

    Hi!
    You can initiate a document with a single line of code, given the $order object:

    
    $invoice = wcpdf_get_document( 'invoice', $order, true );
    

    To do this right away when the order is placed, you can attach this to the woocommerce_checkout_order_processed hook:

    
    add_action('woocommerce_checkout_order_processed', 'wpo_wcpdf_create_invoice_number');
    function wpo_wcpdf_create_invoice_number ( $order_id ) {
    	$order = wc_get_order( $order_id );
    	$invoice = wcpdf_get_document( 'invoice', $order, true );
    }
    

    I wouldn’t recommend this because then you may end up with invoices for orders that are cancelled/failed later. This action basically runs for anyone that presses the ‘Place order’ button in the checkout, regardless of whether payment succeeds.
    To make this more fool proof, you could use an action that ties to a specific order status, like woocommerce_order_status_completed or woocommerce_order_status_processing:

    
    add_action('woocommerce_order_status_completed', 'wpo_wcpdf_create_invoice_number');
    function wpo_wcpdf_create_invoice_number ( $order_id ) {
    	$order = wc_get_order( $order_id );
    	$invoice = wcpdf_get_document( 'invoice', $order, true );
    }
    

    Hope that helps!
    Ewout

    Thread Starter lawkwok

    (@lawkwok)

    That worked, thank you!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Create invoice number when receive order’ is closed to new replies.