Action uncanny-automator-pro

edd_post_insert_discount

Fires after a discount is inserted into the database, providing its data and ID for further processing.

add_action( 'edd_post_insert_discount', $callback, 10, 2 );

Description

Fires after a discount code is successfully inserted into Easy Digital Downloads. Developers can use this hook to perform custom actions, like logging the discount data, associating it with other entities, or triggering additional notifications. It provides both the discount data and the new discount's ID.


Usage

add_action( 'edd_post_insert_discount', 'your_function_name', 10, 2 );

Parameters

$discount_data (mixed)
This parameter contains an array of data used to create the discount, including properties like the discount code, amount, and expiration date.
$discount_id (mixed)
This parameter contains an array of data used to create the discount.

Examples

// Example function to process EDD discount creation after it's inserted.
// This example will log the discount ID and its associated data.
function my_custom_edd_post_discount_logic( $discount_data, $discount_id ) {

    // Check if discount ID and data are available.
    if ( ! empty( $discount_id ) && ! empty( $discount_data ) ) {

        // Log a success message with discount information.
        // In a real-world scenario, you might use WP_Error,
        // a custom logging plugin, or WordPress's built-in logging if available.
        error_log( sprintf( 'Successfully inserted EDD discount with ID: %s. Discount data: %s', $discount_id, print_r( $discount_data, true ) ) );

        // Example: If the discount is a percentage, you might want to
        // perform an additional action or update a meta field.
        // Note: The exact structure of $discount_data depends on how EDD
        // stores discount information. This is a hypothetical example.
        if ( isset( $discount_data['type'] ) && 'percentage' === $discount_data['type'] && isset( $discount_data['amount'] ) ) {
            $percentage = $discount_data['amount'];
            error_log( sprintf( 'Discount %s is a percentage discount of %s%%', $discount_id, $percentage ) );
            // You could potentially add more complex logic here, like
            // updating a custom post meta related to this discount.
        }
    } else {
        // Log an error if essential data is missing.
        error_log( 'edd_post_insert_discount hook triggered but discount_id or discount_data is missing.' );
    }
}

// Hook the custom function to the 'edd_post_insert_discount' action.
// We pass $discount_data and $discount_id, so we set accepted_args to 2.
add_action( 'edd_post_insert_discount', 'my_custom_edd_post_discount_logic', 10, 2 );

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:341

if ( ! $discount_id ) {
			$this->add_log_error( 'Failed to create discount code. Unknown error occurred.' );
			return false;
		}

		// Success - add action hooks
		do_action( 'edd_post_insert_discount', $discount_data, $discount_id );

		// Hydrate tokens so the generated code can be used elsewhere
		$this->hydrate_tokens(
			array(
				'DISCOUNT_CODE'       => $code,
				'DISCOUNT_NAME'       => $name,
				'DISCOUNT_AMOUNT'     => $amount,


Scroll to Top