Creating custom macros

It is possible to create custom macros to be able to output any needed data in the invoice, including the data from other plugins. Below are some examples of how this can be done.

 

Date macro - extract only the part of the date

Let's say we need only the month of the invoice to insert in some text block. This will be possible to show by using {{woo_pdf_custom_macro_date}} macro.

add_filter('woo_pdf_macros', 'woo_pdf_custom_macro_date', 10, 2);

function woo_pdf_custom_macro_date($macros, $order)
{
    $custom_macro_key = 'woo_pdf_custom_macro_date';
    $custom_macro_value = 'key';

    $invoice_date = $macros['{{invoice_date}}'];
    $invoice_date_ts = strtotime($invoice_date);
   $invoice_date_month = date('m', $invoice_date_ts);
   $custom_macro_value = $invoice_date_month;

    $macros[$custom_macro_key] = $custom_macro_value;
    return $macros;
}

Date macro - display the due date of the invoice

Let's try to add the due date to pay the invoice, e.g. 14 days. This will be possible to show by using {{woo_pdf_custom_macro_due_date}} macro.

 

add_filter('woo_pdf_macros', 'woo_pdf_custom_macro_due_date', 10, 2);

function woo_pdf_custom_macro_due_date($macros, $order)
{
    $custom_macro_key = 'woo_pdf_custom_macro_due_date';
    $custom_macro_value = 'key';

    $invoice_date = $macros['{{invoice_date}}'];
    $invoice_date_ts14 = strtotime("+14 day", $invoice_date);
    $invoice_date14 = date('Y-m-d H:i:s', $invoice_date_ts14);
    $custom_macro_value = $invoice_date14;

    $macros[$custom_macro_key] = $custom_macro_value;
    return $macros;
}

 

Have more questions? Submit a request