PDF Invoices & Packing Slips for WooCommerce Shortcode

Below, you’ll find a detailed guide on how to add the PDF Invoices & Packing Slips for WooCommerce Shortcode to your WordPress website, including its parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the PDF Invoices & Packing Slips for WooCommerce Plugin shortcode not to show or not to work correctly.

Before starting, here is an overview of the PDF Invoices & Packing Slips for WooCommerce Plugin and the shortcodes it provides:

Plugin Icon
PDF Invoices & Packing Slips for WooCommerce

"PDF Invoices & Packing Slips for WooCommerce is a dynamic plugin that simplifies your e-commerce operations by automatically generating and sending PDF invoices and packing slips to customers."

★★★★★ (1624) Active Installs: 300000+ Tested with: 6.3.2 PHP Version: 7.1
Included Shortcodes:
  • [wcpdf_download_invoice]

PDF Invoices & Packing Slips for WooCommerce [wcpdf_download_invoice] Shortcode

The ‘wcpdf_download_invoice’ shortcode allows you to generate and download PDF invoices for WooCommerce orders. It uses the ‘generate_document_shortcode’ function to validate document type, fetch the order, and create a download link. If the document type is invalid or the order doesn’t exist, it returns nothing. Otherwise, it returns a URL to download the invoice PDF.

Shortcode: [wcpdf_download_invoice]

Parameters

Here is a list of all possible wcpdf_download_invoice shortcode parameters and attributes:

  • order_id – Identifier for the specific order.
  • link_text – Customizable text for the download link.
  • id – Unique identifier for the shortcode.
  • class – CSS class for the download link.
  • document_type – Type of document to download, default is ‘invoice’.

Examples and Usage

Basic example – A simple shortcode that generates a download link for the invoice of a specific order.

[wcpdf_download_invoice order_id=123 /]

Advanced examples

Generate a download link for the invoice of a specific order, with custom link text.

[wcpdf_download_invoice order_id=123 link_text="Download Your Invoice" /]

Generate a download link for the invoice of a specific order, with custom link text and CSS class for styling.

[wcpdf_download_invoice order_id=123 link_text="Download Your Invoice" class="custom-class" /]

Generate a download link for the invoice of a specific order, with custom link text, CSS class for styling, and a specific document type.

[wcpdf_download_invoice order_id=123 link_text="Download Your Invoice" class="custom-class" document_type="packing_slip" /]

PHP Function Code

In case you have difficulties debugging what causing issues with [wcpdf_download_invoice] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode( 'wcpdf_download_invoice', array( $this, 'generate_document_shortcode' ) );

Shortcode PHP function:

function generate_document_shortcode( $atts, $content = null, $shortcode_tag = '' ) {
		global $wp;

		if ( is_admin() ) {
			return;
		}

		// Default values
		$values = shortcode_atts( array(
			'order_id'      => '',
			'link_text'     => '',
			'id'            => '',
			'class'         => 'wpo_wcpdf_document_link',
			'document_type' => 'invoice',
		), $atts );

		$is_document_type_valid = false;
		$documents              = WPO_WCPDF()->documents->get_documents();
		foreach ( $documents as $document ) {
			if ( $document->get_type() === $values['document_type'] ) {
				$is_document_type_valid = true;

				if ( ! empty( $values['link_text'] ) ) {
					$link_text = $values['link_text'];
				} else {
					$link_text = sprintf(
						/* translators: %s: Document type */
						__( 'Download %s (PDF)', 'woocommerce-pdf-invoices-packing-slips' ),
						wp_kses_post( $document->get_type() )
					);
				}

				break;
			}
		}

		if ( ! $is_document_type_valid ) {
			return;
		}

		// Get $order
		if ( empty( $values['order_id'] ) ) {
			if ( is_checkout() && is_wc_endpoint_url( 'order-received' ) && isset( $wp->query_vars['order-received'] ) ) {
				$order = wc_get_order( $wp->query_vars['order-received'] );
			} elseif ( is_account_page() && is_wc_endpoint_url( 'view-order' ) && isset( $wp->query_vars['view-order'] ) ) {
				$order = wc_get_order( $wp->query_vars['view-order'] );
			}
		} else {
			$order = wc_get_order( $values['order_id'] );
		}

		if ( empty( $order ) || ! is_object( $order ) ) {
			return;
		}

		$document = wcpdf_get_document( $values['document_type'], $order );

		if ( ! $document || ! $document->is_allowed() ) {
			return;
		}

		$pdf_url = WPO_WCPDF()->endpoint->get_document_link( $order, $values['document_type'], [ 'shortcode' => 'true' ] );

		if ( 'wcpdf_document_link' === $shortcode_tag ) {
			return esc_url( $pdf_url );
		}

		return sprintf(
			'<p><a %s class="%s" href="%s" target="_blank">%s</a></p>',
			( ! empty( $values['id'] ) ? 'id="' . esc_attr( $values['id'] ) . '"' : '' ),
			esc_attr( $values['class'] ),
			esc_url( $pdf_url ),
			esc_html( $link_text )
		);
	}

Code file location:

woocommerce-pdf-invoices-packing-slips/woocommerce-pdf-invoices-packing-slips/includes/class-wcpdf-frontend.php

Conclusion

Now that you’ve learned how to embed the PDF Invoices & Packing Slips for WooCommerce Plugin shortcode, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *