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