Skip to content

Hooks and Filters

The WPify Woo Product Vouchers plugin provides filters to customize voucher generation, templates, and coupon data.

wpify_woo_product_vouchers_generate_coupon_data

Section titled “wpify_woo_product_vouchers_generate_coupon_data”

Filter coupon data before the WooCommerce coupon is created.

apply_filters( 'wpify_woo_product_vouchers_generate_coupon_data', array $coupon_data, array $item_data, WC_Order $order );

Parameters:

  • $coupon_data (array) — WooCommerce coupon data (code, amount, usage_limit, expiry_date, etc.)
  • $item_data (array) — Order item data
  • $order (WC_Order) — WooCommerce order object

$coupon_data structure:

  • code — Generated coupon code
  • amount — Coupon value
  • usage_limit — Maximum number of uses
  • date_expires — Expiration date (Y-m-d format)
  • meta_data — Additional coupon metadata

Example — restrict coupon to customer email:

/**
* Restrict coupon to customer billing email
*
* @param array $coupon_data Coupon data
* @param array $item_data Item data
* @param WC_Order $order Order
* @return array
*/
function my_restrict_coupon_to_email( $coupon_data, $item_data, $order ) {
$coupon_data['email_restrictions'] = [ $order->get_billing_email() ];
return $coupon_data;
}
add_filter( 'wpify_woo_product_vouchers_generate_coupon_data', 'my_restrict_coupon_to_email', 10, 3 );

Example — set minimum order amount:

/**
* Set minimum order amount for coupon usage
*
* @param array $coupon_data Coupon data
* @param array $item_data Item data
* @param WC_Order $order Order
* @return array
*/
function my_set_minimum_order( $coupon_data, $item_data, $order ) {
$coupon_data['minimum_amount'] = 500;
return $coupon_data;
}
add_filter( 'wpify_woo_product_vouchers_generate_coupon_data', 'my_set_minimum_order', 10, 3 );

Modify the generated voucher file name.

apply_filters( 'wpify_woo_product_vouchers_file_name', string $file_name, array $voucher, array $template );

Parameters:

  • $file_name (string) — Default file name
  • $voucher (array) — Voucher data
  • $template (array) — Template configuration

Example — custom file name with coupon code:

/**
* Custom voucher file name
*
* @param string $file_name Default name
* @param array $voucher Voucher data
* @param array $template Template
* @return string
*/
function my_voucher_file_name( $file_name, $voucher, $template ) {
return 'gift-voucher-' . $voucher['code'];
}
add_filter( 'wpify_woo_product_vouchers_file_name', 'my_voucher_file_name', 10, 3 );

Modify template data before the voucher image is generated.

apply_filters( 'wpify_woo_product_vouchers_template_data', array $template, WC_Coupon|null $coupon );

Parameters:

  • $template (array) — Template configuration including texts, image file, etc.
  • $coupon (WC_Coupon|null) — WooCommerce coupon object

Example — add a custom text element:

/**
* Add custom text to voucher
*
* @param array $template Template configuration
* @param WC_Coupon|null $coupon Coupon
* @return array
*/
function my_add_voucher_text( $template, $coupon ) {
$template['texts'][] = [
'type' => 'value',
'content' => 'Thank you for your purchase!',
'position_x' => 100,
'position_y' => 200,
'font_size' => 14,
'color' => '#000000',
];
return $template;
}
add_filter( 'wpify_woo_product_vouchers_template_data', 'my_add_voucher_text', 10, 2 );

wpify_woo_product_vouchers_allowed_templates

Section titled “wpify_woo_product_vouchers_allowed_templates”

Filter which templates are available for a specific product.

apply_filters( 'wpify_woo_product_vouchers_allowed_templates', array $allowed_templates, $product, WC_Product $wc_product );

Parameters:

  • $allowed_templates (array) — Array of allowed templates
  • $product (ProductModel) — Plugin product model
  • $wc_product (WC_Product) — WooCommerce product object

Example — restrict templates for premium products:

/**
* Only show premium template for products in premium category
*
* @param array $allowed_templates Allowed templates
* @param mixed $product Product model
* @param WC_Product $wc_product WooCommerce product
* @return array
*/
function my_filter_voucher_templates( $allowed_templates, $product, $wc_product ) {
if ( has_term( 'premium', 'product_cat', $wc_product->get_id() ) ) {
return [ 'premium-template-id' ];
}
return $allowed_templates;
}
add_filter( 'wpify_woo_product_vouchers_allowed_templates', 'my_filter_voucher_templates', 10, 3 );

The plugin provides a shortcode for the template selection page:

[wpify_woo_vouchers_select_voucher]

This renders the voucher template selection and download form on a WordPress page.

You can add custom functions either in your child theme’s functions.php or use the Code Snippets plugin.