Action
uncanny-automator-pro
edd_pre_insert_discount
Fires before a discount is inserted into the database, allowing modification of discount data.
add_action( 'edd_pre_insert_discount', $callback, 10, 1 );
Description
Fires before a discount is inserted into the database. Developers can use this hook to modify discount data, perform additional validation, or trigger other actions before the discount is saved. The `$discount_data` parameter contains the discount information to be inserted.
Usage
add_action( 'edd_pre_insert_discount', 'your_function_name', 10, 1 );
Parameters
-
$discount_data(mixed) - This parameter contains an array of data used to create a new Easy Digital Downloads discount code.
Examples
<?php
/**
* Example function to hook into edd_pre_insert_discount.
*
* This function will add a custom note to the discount data before it's inserted.
*
* @param array $discount_data The data for the discount being created.
* @return array The modified discount data.
*/
function my_custom_edd_pre_insert_discount_logic( $discount_data ) {
// Ensure discount_data is an array and has a 'note' key.
if ( ! is_array( $discount_data ) ) {
return $discount_data; // Return as is if not an array
}
// Add a custom note to the discount data if it doesn't already have one.
if ( ! isset( $discount_data['note'] ) || empty( $discount_data['note'] ) ) {
$discount_data['note'] = 'Discount generated via custom integration.';
} else {
// Append to existing note if it exists.
$discount_data['note'] .= ' (Also processed by custom integration.)';
}
// You could also perform other validations or modifications here.
// For example, ensure a minimum expiration date, or enforce specific discount types.
// Return the modified discount data.
return $discount_data;
}
// Add the action hook.
// The hook is an 'action' hook, but in practice, many such hooks in EDD
// are used for pre-processing and expect the modified data to be returned.
// Therefore, we will treat it as if it were a filter for this example,
// even though the documentation states 'action'. If it truly only expects
// actions and doesn't use the return value, the logic would be different
// (e.g., directly modifying global state or performing side effects).
// For demonstration purposes, assuming it's designed to allow modification
// of the data being passed to edd_add_discount.
add_action( 'edd_pre_insert_discount', 'my_custom_edd_pre_insert_discount_logic', 10, 1 );
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/edd/actions/edd-generate-discount-code.php:324
// Validate product condition
if ( ! empty( $discount_data['product_condition'] ) && ! in_array( $discount_data['product_condition'], array( 'any', 'all' ), true ) ) {
$this->add_log_error( 'Product condition must be either "any" or "all".' );
return false;
}
// Add action hooks
do_action( 'edd_pre_insert_discount', $discount_data );
// Create the discount
$discount_id = edd_add_discount( $discount_data );
// Check for errors
if ( is_wp_error( $discount_id ) ) {
$this->add_log_error( sprintf( 'Failed to create discount code: %s', $discount_id->get_error_message() ) );