Skip to content

Developer Guide [en_US]

Amirhossein Hosseinpour edited this page Aug 29, 2024 · 1 revision

Customizing PeproDev Ultimate Invoice: Templates and Macros

Introduction

Welcome to the developer's guide for customizing the PeproDev Ultimate Invoice plugin. This guide will help you add custom invoice templates and macros to enhance the functionality of your invoices. Whether you need to create a unique template or add dynamic content, this guide has got you covered.

Adding a Custom Invoice Template

To add a custom invoice template, follow these steps:

  1. Copy an Existing Template:

    • First, copy one of the existing templates provided by the PeproDev Ultimate Invoice plugin.
  2. Modify the Template:

    • Make the necessary changes to your copied template as per your requirements.
  3. Register the New Template:

    • Use the following code snippet to register your new template with the plugin. Place this code in your theme's functions.php file or a custom plugin.
/**
* Add custom template to Invoice Templates list
* You can later select this Template from Ultimate Invoice Theming Settings
* 
* Snippet by: Amirhp.Com
* Tested upto: WooCommerce: 9.x / WordPress: 6.6.x / Ultimate Invoice: 2.x
*/
add_filter("puiw_get_templates_list", function ($templates) { 
   $templates[] = plugin_dir_path(__FILE__) . "invoice-template/default.cfg"; 
   return $templates;
}, 10, 1);
  1. Set the New Template:
    • Go to the Ultimate Invoice settings and select the newly added template as your default invoice template.

Adding Custom Macros

1. Adding Dynamic Parameters to an Invoice Template

If you need to add new dynamic parameters (macros) to your invoice template, follow these steps:

/**
* Add Custom dynamic params to Invoice Template
* Use as {{{delivery_date}}} and {{{delivery_barcode}}} 
* 
* Snippet by: Amirhp.Com
* Tested upto: WooCommerce: 9.x / WordPress: 6.6.x / Ultimate Invoice: 2.x
*/
add_filter("puiw_get_default_dynamic_params", function($array_macros, $wc_order) {
   $order = wc_get_order($wc_order);
   $opts["delivery_date"] = $order->get_meta("_shipping_puiw_invoice_shipdate", true);
   $opts["delivery_barcode"] = $order->get_meta("_shipping_puiw_invoice_track_id", true);
   return $array_macros;
}, 10, 2);

2. Preserving English Digits in Macros

To ensure that certain macros retain their English digits (e.g., for tracking numbers or codes), use the following hook:

/**
* Add {{{delivery_barcode}}} to Preserved Numbers as English digits list
* 
* Snippet by: Amirhp.Com
* Tested upto: WooCommerce: 9.x / WordPress: 6.6.x / Ultimate Invoice: 2.x
*/
add_filter("puiw_printinvoice_preserve_english_numbers", function($array_macros, $options, $wc_order) {
   array_push($array_macros, "delivery_barcode");
   return $array_macros;
}, 10, 3);

3. Preserving HTML in Macros

If you need to prevent the sanitization of HTML within a macro, use the following snippet:

/**
* Add {{{delivery_barcode}}} to Preserved HTML Tags list
* If this macro contain HTML elements, it won't be sanitized
* 
* Snippet by: Amirhp.Com
* Tested upto: WooCommerce: 9.x / WordPress: 6.6.x / Ultimate Invoice: 2.x
*/
add_filter("puiw_printinvoice_preserve_html_tags", function($array_macros, $options, $wc_order) {
   array_push($array_macros, "delivery_barcode");
   return $array_macros;
}, 10, 3);

Conclusion

We hope this guide helps you customize the PeproDev Ultimate Invoice plugin to suit your needs. If you have any questions or run into any issues, feel free to reach out to us.