Download Invoice link with shortcode

PDF Invoices & Packing Slips for WooCommerce comes with some useful shortcodes to place a Download Invoice link in other pages, where you have access to the order ID. Alternatively, you define the ID inside the order_id attribute.

Available shortcodes #

Currently, you can use these shortcodes:

  • [wcpdf_download_pdf] — This shortcode return a text with the PDF document link. By default, the PDF invoice link will be displayed, but you can set the document type as an attribute (see Setting up the shortcode). There is an alias for this shortcode which behaves in exactly the same way: [wcpdf_download_invoice]
  • [wcpdf_document_link] — This shortcode returns the raw PDF document link. It is useful to link an image with the PDF document, use it within a button, etc.

Setting up the shortcodes #

[wcpdf_download_pdf] attributes #

The [wcpdf_download_pdf] shortcode receive some optional attributes that you can use to customize the output:

  • document_type="invoice" — allows you to pass the document type: invoice, packing-slip, etc. If the attribute is not set, the PDF invoice will be selected.
  • link_text="Download Invoice" — allows you to customize the shortcode output text. By default, it is e.g. Download Invoice (PDF), where “Invoice” is the document name.
  • order_id="123" — accepts an order number. If the shortcode is placed in the “Thank you” page or the order details page, within the “My account” area, this attribute is optional.
  • id="invoice-link" — you can set an ID which you can use to create an anchor link, for instance.
  • class="pdf-invoice pdf-shortcode" — you can set your own classes within this attribute, which will helps you to apply style customization to the output.

[wcpdf_document_link] attributes #

On the other hand, the [wcpdf_document_link] only receive an optional attribute, the order ID:

  • order_id="123" — accepts an order number. If the shortcode is placed in the “Thank you” page or the order details page, within the “My account” area, this attribute is optional.

Usage examples #

[wcpdf_download_pdf] shortcode with default attributes #

[wcpdf_download_pdf]

It will display the Download invoice (PDF) text, linked to the PDF document.

[wcpdf_download_pdf] shortcode with all attributes customized #

[wcpdf_download_pdf document_type="invoice" link_text="Click here to Download the PDF invoice" order_id="123" id="invoice-link" class="pdf-invoice pdf-shortcode"]

It will display the following customized text: Click here to Download the PDF invoice (linking to the PDF document), and also include the ID and classes set within the shortcode.

[wcpdf_document_link] shortcode #

[wcpdf_document_link]

It will display the raw document link, like the following:

https://example.com/wp-admin/admin-ajax.php?action=generate_wpo_wcpdf&document_type=invoice&order_ids=123&access_key=1234567890&shortcode=true

Inserting the shortcodes into a PHP file #

If you’re working directly in template files, you can also use this PHP code:

<?php echo do_shortcode('[wcpdf_download_pdf]'); ?>

Printing the shortcodes on the “Thank You” page #

In theory, the shortcodes can be used on any page you can edit on WordPress, but the “Thank You” page (also called the “Order received” page sometimes) is not editable by default (though there are various plugins out there that can enable this).

An alternative method to add the shortcodes within the “Thank You” page is to use a small code snippet like the following:

add_action( 'woocommerce_thankyou', function() {
    echo do_shortcode('[wcpdf_download_pdf]');
});

If you haven’t worked with code snippets (actions/filters) or functions.php before, read this guide: How to use filters​​​.