Filter
uncanny-automator-pro
woocommerce_coupon_discount_types
Filters coupon discount types, allowing modification of available discount options in WooCommerce.
add_filter( 'woocommerce_coupon_discount_types', $callback, 10, 1 );
Description
Filters the array of available WooCommerce coupon discount types. Developers can use this hook to add, remove, or modify coupon types available for selection, for example, to support custom coupon functionalities or limit options. This hook fires after WooCommerce retrieves all standard coupon types.
Usage
add_filter( 'woocommerce_coupon_discount_types', 'your_function_name', 10, 1 );
Parameters
-
$coupon_types(mixed) - This parameter contains an array of coupon types available in WooCommerce, intended to be filtered and modified by other plugins or themes.
Return Value
The filtered value.
Examples
add_filter( 'woocommerce_coupon_discount_types', 'my_custom_woocommerce_coupon_discount_types', 10, 1 );
/**
* Add a custom coupon discount type or modify existing ones.
*
* This function demonstrates how to hook into the 'woocommerce_coupon_discount_types'
* filter to add a new discount type or manipulate the existing ones provided by WooCommerce.
* In this example, we'll add a hypothetical "Amount per item" discount type.
*
* @param array $coupon_types An array of existing coupon discount types.
* Keys are the internal type slugs, values are the display labels.
* @return array The modified array of coupon discount types.
*/
function my_custom_woocommerce_coupon_discount_types( $coupon_types ) {
// Let's say WooCommerce already provides these:
// $coupon_types = array(
// 'fixed_cart' => __( 'Fixed cart discount', 'woocommerce' ),
// 'percent_cart' => __( 'Percentage cart discount', 'woocommerce' ),
// 'fixed_product' => __( 'Fixed product discount', 'woocommerce' ),
// 'percent_product' => __( 'Percentage product discount', 'woocommerce' ),
// );
// Add a new custom discount type.
// The key 'amount_per_item' is a unique identifier for this new type.
// The value 'Amount per item discount' is what will be displayed in the dropdown.
$coupon_types['amount_per_item'] = __( 'Amount per item discount', 'my-text-domain' );
// Alternatively, you could modify an existing type, for example,
// changing the label for 'fixed_cart':
// if ( isset( $coupon_types['fixed_cart'] ) ) {
// $coupon_types['fixed_cart'] = __( 'Special Fixed Cart Discount', 'my-text-domain' );
// }
// Or remove a type entirely (though less common and potentially disruptive):
// unset( $coupon_types['percent_product'] );
return $coupon_types;
}
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
uncanny-automator-pro/src/integrations/woocommerce/triggers/wc-type-of-coupon-created.php:248
private function get_coupon_types() {
$types = array(
array(
'text' => esc_html_x( 'Any type', 'WooCommerce', 'uncanny-automator-pro' ),
'value' => '-1',
),
);
// Get coupon types from WooCommerce
$coupon_types = wc_get_coupon_types();
// Apply filter to get all available discount types
$discount_types = apply_filters( 'woocommerce_coupon_discount_types', $coupon_types );
foreach ( $discount_types as $type => $label ) {
$types[] = array(
'text' => $label,
'value' => $type,
);
}
return $types;
}