• Hi,
    I use the below code which is a simple method to create a custom order from my functions.php file.

    $user = wp_get_current_user();
    $order = new WC_Order();
    $order->set_customer_id($user->ID);
    $product = wc_get_product(123);
    $order->add_product($product);
    $order->save();

    Is there a way to generate also the invoice details (Invoice Number and Invoice Date)?
    Image: https://i.ibb.co/yqRDXzS/2019-05-17-02-39-44.png

    Thank you,

    Yiannis

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter Yiannis Christodoulou

    (@yiannistaos)

    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();
    
    Thread Starter Yiannis Christodoulou

    (@yiannistaos)

    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.

    Thread Starter Yiannis Christodoulou

    (@yiannistaos)

    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

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Generate PDF after a custom order through PHP’ is closed to new replies.