Last active
May 2, 2017 20:40
-
-
Save justinstern/7109574 to your computer and use it in GitHub Desktop.
Sample code demonstrating how to create custom voucher fields for with the WooCommerce PDF Product Vouchers plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Put the following in your theme's functions.php for instance: | |
add_filter( 'wc_pdf_product_vouchers_voucher_fields', 'wc_pdf_product_vouchers_add_custom_fields', 10, 2 ); | |
/** | |
* Adds some custom fields to the given product voucher | |
* | |
* @param array $voucher_fields associative array of voucher fields | |
* @param WC_Voucher $voucher voucher object | |
* @return array associative array of voucher fields | |
*/ | |
function wc_pdf_product_vouchers_add_custom_fields( $voucher_fields, $voucher ) { | |
// defining a custom field. Any number of fields can be added in this manner | |
$voucher_fields['voucher_my_custom_field'] = array( | |
'type' => 'property', | |
'font' => array( | |
'family' => '', // empty for voucher default, otherwise one of Helvetica, Courier or Times | |
'size' => '', // empty for voucher default, otherwise font size | |
'style' => '', // empty for voucher default, otherwise one of 'B', 'I' or 'BI' for bold, italic or bold-italic, respectively | |
'color' => '', // empty for voucher default, otherwise a hex color code, ie '#1e73be' | |
), | |
'position' => array( | |
'x1' => '9', // x position from top left | |
'y1' => '253', // y position from top left | |
'width' => '493', // width of field | |
'height' => '42', // height of field | |
), | |
'order' => '', // not used | |
'multiline' => false, // whether the field should be rendered as multiline | |
); | |
return $voucher_fields; | |
} | |
// Setting a value for the custom field upon order creation | |
add_action( 'woocommerce_add_order_item_meta', 'wc_pdf_product_vouchers_set_custom_fields', 10, 2 ); | |
/** | |
* Set values for any custom fields | |
* | |
* @param int $item_id the item identifier | |
* @param array $values order information | |
*/ | |
function wc_pdf_product_vouchers_set_custom_fields( $item_id, $values ) { | |
// use the name of your custom field from above, prepended with '_', ie 'voucher_my_custom_field' becomes '_voucher_my_custom_field' | |
woocommerce_add_order_item_meta( $item_id, '_voucher_my_custom_field', 'my custom field value' ); | |
} | |
// The following would not go in your functions.php, and demonstrates how to retrieve the custom field value using the standard woocommerce_get_order_item_meta() method | |
// This is after you've obtained an item_id for an order item containing a product voucher: | |
woocommerce_get_order_item_meta( $item_id, '_voucher_my_custom_field', true ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There's an error in the main
class-wc-voucher.php
: passed text in function.php change by default to "ucwords" (each word is capitalized) in the pdf.To fix this, edit the file
includes/class-wc-voucher.php
and change the value:to
at lines 1056 and 1058
And finally you can add more custom text in your
wc_pdf_product_vouchers_add_custom_fields()
function.Like this: