WooCommerce integration

How to Add Custom Variable

This functionality is one of the PRO features and works only in the PRO version.

All the changes will be made in a separate plugin. This allows you to avoid conflicts with upcoming plugin updates.

1. Create a new plugin

For our new plugin, we will use the name oxilayer-custom-var. To create a new WordPress plugin, create the following file

WP_DIR/wp-content/plugins/oxilayer-custom-var/oxilayer-custom-var.php

and paste this content

<?php
/*
Plugin Name: Oxilayer PDF Invoice Custom Variable
Plugin URI: https://www.oxilayer.com/
Description: Oxilayer PDF Invoice Custom Variable
Version: 0.0.1
Requires at least: 5.0
Requires PHP: 5.2
Author: MB Vienas bitas
Author URI: https://www.oxilayer.com/
License: GPLv2 or later
Text Domain: oxilayer-custom-var
*/

2. Activate plugin

To activate the plugin, log in to your WordPress admin account and navigate to

WP Admin > Plugins > Installed Plugins

There should be the new plugin added to this list called 'Oxilayer PDF Invoice Custom Variable' as shown in the image below. Click on the link 'Activate'..

3. Available Hooks

The Oxilayer PDF invoice plugin already has prepared hooks, so all we have to do is hook up our function and provide data. The following hooks are available

  • oxi_pdf_custom_variable - for all PDF documents
  • oxi_pdf_order_custom_variable - for order PDF only
  • oxi_pdf_invoice_custom_variable - for invoice PDF only
  • oxi_pdf_shipment_custom_variable - for shipment PDF only
  • oxi_pdf_refund_custom_variable - for refund PDF only

4. Add Action

Below is an example of code with comments on how to use one of the hooks and add a new variable. In this example, we chose to add the {customer_id} variable. The following code should be used in the file created in the first step.

WP_DIR/wp-content/plugins/oxilayer-custom-var/oxilayer-custom-var.php

defined('ABSPATH') || exit;

add_action('oxi_pdf_custom_variable', function ($dataCollector)
{
    //Get order id
    $orderId = $dataCollector->getData('order_id');

    //Load order with
    $order = wc_get_order($orderId);

    //Set value as empty
    $customerId = '';

    //Check, if order exist
    if ($order) {
        //Extract data you need from order object
        $customerId = $order->get_customer_id();;
    }

    //Register new variable
    $dataCollector->setData('customer_id', $customerId);
});

5. Testing

If everything has been done correctly, you should see your new variable in the editor here:

WP Admin > WooCommerce > Oxilayer PDF Invoice > Open in Editor > Variables

If you add this variable in the PDF template and generate an invoice, it should be replaced with the real customer ID.

Here is the full code of this small plugin available for download on GitHub.