Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save justinstern/7109574 to your computer and use it in GitHub Desktop.
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
<?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 );
@fl1digital
Copy link

Hey! This is great and I'm using it on my site.

I have some custom fields already stored in the wp_postmeta table... is there a way of displaying those on the generated PDF without having to store them again on the order_itemmeta? Been looking at class-wc-voucher.php for clues but can't find anything. From what I could see, custom field values are retrieved from order_itemmeta, but then I realised the product title (product_name) that's printed on the PDF isn't stored there, which means there might be a way of getting custom field data from wp_postmeta by using the "_product_id" value from order_itemmeta?

I really don't want to hack core files so if there's a way it'd be great if you could point me in the right direction :)

Thanks
Alex

@gubi
Copy link

gubi commented Aug 25, 2015

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:

$this->$field_name

to

$this->voucher_fields[strtolower($this->$field_name)]["label"]

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:

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['hello'] = 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'     => '45',   // x position from top left
            'y1'     => '279', // y position from top left
            'width'  => '493', // width of field
            'height' => '42',  // height of field
        ),
        'order'     => '',     // not used
        'label'     => 'Hello, I'm the first custom text here',    // your text here
        'multiline' => false,  // whether the field should be rendered as multiline
    );
    $voucher_fields['hello2'] = 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'     => '46',   // x position from top left
            'y1'     => '480', // y position from top left
            'width'  => '150', // width of field
            'height' => '42',  // height of field
        ),
        'order'     => '',     // not used
        'label'     => 'Hello, this is the second message',    // your text here
        'multiline' => true,  // whether the field should be rendered as multiline
    );
    $voucher_fields['hello3'] = 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'     => '150',   // x position from top left
            'y1'     => '550', // y position from top left
            'width'  => '100', // width of field
            'height' => '42',  // height of field
        ),
        'order'     => '',     // not used
        'label'     => 'And this is the third...',    // your text here
        'multiline' => false,  // whether the field should be rendered as multiline
    );
    // And so on...

    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 );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment