Invoices for WooCommerce Shortcode

Below, you’ll find a detailed guide on how to add the Invoices 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 Invoices for WooCommerce Plugin shortcode not to show or not to work correctly.

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

Plugin Icon
Invoices for WooCommerce

"Invoices for WooCommerce is a handy plugin that generates professional PDF invoices and packing slips for WooCommerce orders, simplifying your ecommerce business operations."

★★★★☆ (468) Active Installs: 20000+ Tested with: 5.8.8 PHP Version: false
Included Shortcodes:
  • [bewpi-download-invoice]

Invoices for WooCommerce [bewpi-download-invoice] Shortcode

The ‘bewpi-download-invoice’ shortcode is designed to generate a downloadable invoice for a specific order in WooCommerce. This shortcode takes an ‘order_id’ as an attribute and generates an invoice for that order. If no ‘order_id’ is provided or the order is not found, it returns nothing. It creates a new BEWPI_Invoice object with the ‘order_id’ and retrieves the invoice’s full path. The shortcode further checks if the order is paid by using a filter ‘wpi_show_download_invoice_shortcode’. If the order is not paid, it returns nothing. It then replaces placeholders in the title with actual values from the invoice and order. Finally, it creates a URL with query arguments and prints an HTML anchor tag with the URL and title. The ‘bewpi_action’ query argument is set to ‘view’, which means the invoice will be displayed when the link is clicked.

Shortcode: [bewpi-download-invoice]

Parameters

Here is a list of all possible bewpi-download-invoice shortcode parameters and attributes:

  • order_id – The unique identifier of the specific order
  • title – The text that will be displayed as the link to download the invoice

Examples and Usage

Basic Example – This shortcode allows the download of an invoice related to a specific order by referencing the order ID.

[bewpi-download-invoice order_id=1234 /]

Advanced Examples

The shortcode can also display the invoice download link with a custom title. The title can include placeholders for the formatted invoice number, order number, formatted invoice date, and formatted order date. These placeholders will be replaced with the actual values when the shortcode is processed.

[bewpi-download-invoice order_id=1234 title="Invoice {formatted_invoice_number} for Order {order_number} dated {formatted_order_date}" /]

In another example, you can use the shortcode to display the invoice download link without showing the order date. This can be useful when you want to keep the display simple and focused on the invoice and order numbers.

[bewpi-download-invoice order_id=1234 title="Invoice {formatted_invoice_number} for Order {order_number}" /]

PHP Function Code

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

Shortcode line:

add_shortcode( 'bewpi-download-invoice', array( $this, 'download_invoice_shortcode' ) );

Shortcode PHP function:

function download_invoice_shortcode( $atts ) {
			if ( ! isset( $atts['order_id'] ) || 0 === intval( $atts['order_id'] ) ) {
				return;
			}

			$order_id = (int) $atts['order_id'];
			$invoice  = new BEWPI_Invoice( $order_id );
			if ( '' === $invoice->get_full_path() ) {
				return;
			}

			$order = wc_get_order( $order_id );
			// By default order status should be Processing or Completed.
			if ( false === apply_filters( 'wpi_show_download_invoice_shortcode', $order->is_paid(), $invoice ) ) {
				return;
			}

			$tags = array(
				'{formatted_invoice_number}' => $invoice->get_formatted_number(),
				'{order_number}'             => $order_id,
				'{formatted_invoice_date}'   => $invoice->get_formatted_date(),
				'{formatted_order_date}'     => $invoice->get_formatted_order_date(),
			);
			// Find and replace placeholders.
			$title = str_replace( array_keys( $tags ), array_values( $tags ), $atts['title'] );

			$args = array(
				'bewpi_action' => 'view',
				'post'         => $order_id,
				'nonce'        => wp_create_nonce( 'view' ),
			);
			$url  = add_query_arg( $args );

			printf( '<a href="%1$s">%2$s</a>', esc_attr( $url ), esc_html( $title ) );
		}

Code file location:

woocommerce-pdf-invoices/woocommerce-pdf-invoices/includes/woocommerce-pdf-invoices.php

Conclusion

Now that you’ve learned how to embed the Invoices 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 *