Ok, this did my job. But, feel free to share any other ideas.
I don’t know if it is the right method, but it works like a charm 🙂
// WooCommerce PDF Invoices & Packing Slips
// Generate PDF after the order
use WPO\WC\PDF_Invoices\Compatibility\Order as WCX_Order;
if( class_exists( 'WooCommerce_PDF_Invoices' ) )
{
WCX_Order::update_meta_data( $order, '_wcpdf_invoice_number', 987 );
WCX_Order::update_meta_data( $order, '_wcpdf_formatted_invoice_number', 987 );
WCX_Order::update_meta_data( $order, '_wcpdf_invoice_date', date_i18n( get_option( 'date_format' ), strtotime( '2014-09-20' ) ) );
}
Plugin Contributor
Ewout
(@pomegranate)
That works, but it’s relatively static (and those WCX_Order methods are primarily there for backwards compatibility with WooCommerce lower than 3.0, you can use use $order->update_meta_data() in current versions).
You can use the $invoice object instead:
$invoice = wcpdf_get_invoice( $order, true );
In this case the ‘next invoice number’ would still be incremented with the value that was set automatically.
This automatically stores all the meta data in the order (for full compatibility it also needs the _wcpdf_invoice_number_data, and _wcpdf_invoice_date should actually be a timestamp rather than a formatted date, which is stored in _wcpdf_invoice_date_formatted).
If you want to set the invoice number and date manually you can leave out the true parameter for automatic init and use:
$invoice = wcpdf_get_invoice( $order );
$invoice->set_number( 987 );
$invoice->set_date( strtotime( '2014-09-20' ) );
$invoice->save();
Hi Ewout,
Thanks for your suggestions.
This does not working for me. Where is this class located? “Order_Document_Methods”
I’ve got the below error:
Fatal error: Class ‘WPO\WC\PDF_Invoices\Documents\Order_Document_Methods’ not found in /wp/wp-content/plugins/woocommerce-pdf-invoices-packing-slips/includes/legacy/class-wcpdf-legacy-document.php on line 23
Plugin Contributor
Ewout
(@pomegranate)
In that case you’re calling this too early. These functions are not available until init 15. Try moving your code to init 20, this should resolve the error.
Hi,
Many thanks, Ewout.
Finally this worked great:
$order->update_meta_data( '_wcpdf_invoice_number', 123 );
$order->update_meta_data( '_wcpdf_formatted_invoice_number', 123 );
$order->update_meta_data( '_wcpdf_invoice_date', date_i18n( get_option( 'date_format' ), strtotime('2019-05-27 14:30:00') ) );
Do not forget to put the above code before the order save!
$order->save();
Thank you.
Regards,
Yiannis