Support » Plugin: WooCommerce PDF Invoices & Packing Slips » Manually generate PDF invoice with function

  • Resolved specex

    (@specex)


    I have a WP-Lister Pro for Amazon installed, when the order is completed it won’t auto generate the PDF invoice. For other standard Woocommerce’s order is working good and pdf is generated.

    Can you suggest how i can trigger the pdf once it has been completed by custom function or hook? Thanks

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Support kluver

    (@kluver)

    Hi Specex,

    Yes this is possible. Please add the following to your functions.php:

    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 );
    }

    If you have never worked with code snippets or functions.php pleas read this first: How to use filters

    I hope this answers your question!

    With kind regards,

    Michael

    Thanks for quick reply.

    The above code trigger a blank screen after i click the Complete order button. After refresh the order page again the order went to completed but no PDF generated. Perhaps is caused by old version plugin?

    I’m using
    WooCommerce PDF Invoices & Packing Slips 1.5.37
    WooCommerce PDF Invoices & Packing Slips Professional 1.4.2
    Woocommerce 2.6.4

    Thanks in advance

    Plugin Author Ewout

    (@pomegranate)

    Hi! That code is for WooCommerce PDF Invoices & Packing Slips 2.0 or higher, so that explains why this didn’t work for you. Unfortunately we do not have such a function for 1.5.

    Hi there!

    Sorry to open this thread again. I was wondering, how I use the Class object returned from wcpdf_get_document.

    In my case, I need to trigger the PDF generation if my conditional is met. So if vat number is filled in in Checkout, I want to add the PDF attachment to the customer order mail.

    The steps where I need some help are in the code comments below. Could you please help me on this?

    Anyhow, loving the plugin tho 🙂

    
    function xyz_checkout_fields_validation($order_id){
    	if(isset($_POST['vat_number'])){
    		$order = wc_get_order( $order_id );
    		$invoice = wcpdf_get_document( 'invoice', $order, true );
                    // $pdf = generatePDF($invoice)
                    // add $pdf as attachment
    	}
    }
    
    Plugin Author Ewout

    (@pomegranate)

    Hi! This function is just for generating the PDF, to handle attachments you need to approach it differently, as you can’t shoot the file out into the wild to attach it to an email 🙂

    The easiest way to do this is to enable the PDF attachment in the settings and then use the wpo_wcpdf_custom_attachment_condition filter to disable it for orders that don’t have a vat number (note that you have to query the order for this as the email will not always be sent out at the same time the checkout is processed).
    This thread should get you started: https://wordpress.org/support/topic/choose-whether-to-send-the-invoice/

    Hope that helps!
    Ewout

    Awesome! I will try this out 🙂

    Thanks for the quick and solid advice!

    Greets

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Manually generate PDF invoice with function’ is closed to new replies.