Skip to content

Instantly share code, notes, and snippets.

@ragulka
Created March 10, 2017 07:19
Show Gist options
  • Save ragulka/90611f6e0d0d127949d17ead0b675e62 to your computer and use it in GitHub Desktop.
Save ragulka/90611f6e0d0d127949d17ead0b675e62 to your computer and use it in GitHub Desktop.
Add custom fields to PDF Product Vouchers
<?php
// add custom voucher fields - see: https://cl.ly/323U011F0C2u
add_filter( 'wc_pdf_product_vouchers_voucher_fields', 'my_voucher_fields' );
function my_voucher_fields( $fields ) {
$fields['my_property'] = array(
'data_type' => 'property',
'label' => __( 'My property', 'my-plugin' ),
);
$fields['my_number_input'] = array(
'data_type' => 'user_input',
'type' => 'number',
'label' => __( 'My number input', 'my-plugin' ),
);
return $fields;
}
// return a custom value for a property field. by default, the value will
// be loaded from voucher post meta (get_post_meta( $voucher_id, '_' . $field_id, true ) )
add_filter( 'wc_pdf_product_vouchers_get_my_property', 'my_property_value' );
function my_property_value( $value ) {
$value = 'my property value';
return $value;
}
// format a custom voucher field value - see result: https://cl.ly/0v2x073i3B1w
add_filter( 'wc_pdf_product_vouchers_get_my_property_formatted', 'my_property_value_formatted' );
function my_property_value_formatted( $value_formatted ) {
$value_formatted = '<strong>' . ucfirst( $value_formatted ) . '</strong>';
return $value_formatted;
}
// To customize the voucher HTML template, copy woocommerce-pdf-product-vouchers/templates/voucher/voucher.php to your
// theme: my-theme/voucher/voucher.php and customize it to your needs. Note: this template is both by the voucher template
// and the generated vouchers.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment