How to use the Custom Function block

The Customizer of Premium Templates extension provides this Custom Function block in order to display non-common information, or specific requests.

How it works #

Summary #

  1. Add a ‘Custom function‘ block via the Customizer.
  2. Add a function name to the block.
  3. Paste the code snippet with the function name into your site.

More details #

Go to WooCommerce > PDF Invoices > Customizer.

Add a ‘Custom function‘ block.

Add a function name to the block.

Then finally, make sure the text matches the name of the function you are writing from the site’s functions.php file or using a code snippet plugin.

Below is an image of the code snippet pasted via the Code Snippets plugin. The function name is highlighted in the red box. This is always after the ‘function‘ keyword.

Examples #

PayPal payout data example #

In the case of the PayPal fees example, the block should be set up like in the image below, with a value for the Function name filled in.

The text:
wcpdf_show_paypal_fees

Remember that this is a drag-and-drop Customizer, so the position of the Custom function on the PDF is determined by the position set here. Below it is set as the last item in the Total Rows. This means that the output custom data will appear last, in the totals.

The next step is to paste the code snippet that will output our custom data.

The name of the function that will be pasted must be the same as the text we entered in the Custom Function block via the Customizer. This way, this Custom function block will know to which function it should connect.

Full code snippet:

   function wcpdf_show_paypal_fees( $document ) {
        if ( empty( $document->order ) ) { return; }
        $order      = $document->order;

        $paypal_fee = $order->get_meta( '_paypal_fee' );
        $paypal_net = $order->get_meta( '_paypal_net' );

        $formatted_paypal_fee = format_with_decimal_coma_2_decimal_places( $paypal_fee );
        $formatted_paypal_net = format_with_decimal_coma_2_decimal_places( $paypal_net );

        if ( $formatted_paypal_fee ) {

            return [
                [
                    'label' => 'Paypal Fee',
                    'value' => $formatted_paypal_fee . " " . get_woocommerce_currency_symbol(),
                ], [
                    'label' => 'Paypal Net Payout',
                    'value' => $formatted_paypal_net . " " . get_woocommerce_currency_symbol(),
                ],
            ];
        }
    }

    function format_with_decimal_coma_2_decimal_places( $string ) {
        $TWO_DECIMAL_PLACES      = 2;
        $COMMA_DECIMAL_SEPARATOR = ',';
        $formatted_string  = number_format( (float) $string, $TWO_DECIMAL_PLACES, $COMMA_DECIMAL_SEPARATOR );

        return $formatted_string;
    }

The result on the PDF, viewing the Totals:

Change the position of the block via the Customizer to adjust it in the PDF.

Result:

Thanks to the Custom function block, we can handle very specific requests, adding the custom data we want to the totals of our PDFs as an example.

Display additional currency example #

This example uses the CURCY Multi-Currency plugin for WooCommerce plugin in order to retrieve the exchange rates.

In this example, we are interested in displaying the Grand Total in Euros alongside the Grand Total in Polish Zloty. This however should only function for orders with a billing country of Poland.

With this CURCY plugin, the exchanges rates are found at Multi Currency > Multi Currency > General, then scrolling down to Currency Options.

The code snippet to paste into your site:

/**
 * WP Overnight PDF Invoices & Packing Slips. Show other currency from this plugin:
 * https://wordpress.org/plugins/woo-multi-currency/
**/
function wpo_wcpdf_add_custom_currency ( $document ) {
    if ( $order = $document->order ) {
        // get the current order's currency 3 (letter code)
        $order_currency = $order->get_currency();

        if ( function_exists( 'wmc_get_exchange_rate' ) && $order_currency !== 'EUR' ) {
            $exchange_rate = wmc_get_exchange_rate( 'PLN' );
            $order_total = (float) $order->get_total();
            $total_2 = $order_total * ( 1 / $exchange_rate); 

            $custom_currency = array(
                'label' => "Total",
                'value' => '<b>' . get_woocommerce_currency_symbol( $currency = 'EUR' ) . number_format( $total_2, 2, '.', '' ) . '</b>'
            );

            return $custom_currency;
        }
    }
}

From the code snippet above, we can observe that the function name is…

wpo_wcpdf_add_custom_currency

…so that is what shall be inserted into the Function name textbox.

Result on PDF:

An image of an additional currency on pdf

It can be observed in the image above that all price amounts use Zloty except the data we added, in Euros.