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 = 'no value';
$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 = 'no value';
$invoice_date = $macros['{{invoice_date}}'];
$invoice_date_ts14 = strtotime($invoice_date);
$invoice_date_ts14 = strtotime("+14 day", $invoice_date_ts14);
$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;
}
A shorter version of the above:
add_filter('woo_pdf_macros', 'woo_pdf_custom_macro_due_date', 10, 2);
function woo_pdf_custom_macro_due_date($macros, $order)
{
$macros['{{woo_pdf_custom_macro_due_date}}'] = date('Y-m-d H:i:s', strtotime("+14 day", strtotime($macros['{{invoice_date}}'])));
return $macros;
}
Extracting Product Property from WooCommerce Custom Fields plugin
This is the example of how to show the value of custom Product Property value from our WooCommerce Custom Fields plugin (using with {{woo_pdf_custom_macro_wccf_pp}} macro in this case):
add_filter('woo_pdf_macros', 'woo_pdf_custom_macro_wccf_pp', 10, 2); function woo_pdf_custom_macro_wccf_pp($macros, $order) { // Configure the parameters $custom_macro_key = '{{woo_pdf_custom_macro_wccf_pp}}'; $custom_macro_value = 'no value'; // default value // Check if order is actually an object if (is_object($order)) { $order_items = $order->get_items(); foreach ($order_items as $item_id => $item) { $product_id = $item->get_product_id(); $custom_value_array = wccf_get_values(array( 'key' => 'some_property', 'context' => 'product_prop', 'item_id' => $product_id, 'include_meta' => true )); $custom_value = $custom_value_array['value'][0]; if (!empty($custom_value)) { if ($custom_macro_value == 'no value') { $custom_macro_value = $custom_value; } else { $custom_macro_value .= $custom_value; } } } } // Add custom macro and return the whole array $macros[$custom_macro_key] = $custom_macro_value; return $macros; }
Similarly, you can integrate any data from some other plugin - just use its default methods to extract data and then use it in PDF Invoice.