How to Disable Emails for Free Purchases in Easy Digital Downloads

Home » Snippets » How to Disable Emails for Free Purchases in Easy Digital Downloads
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Are you using Easy Digital Downloads on your WordPress site and finding it overwhelming to manage the flood of email notifications for free purchases? If so, you’re in luck! In this article, we’ll provide you with a step-by-step guide on how to disable email notifications for free purchases in Easy Digital Downloads. By implementing this simple solution, you’ll be able to streamline your email inbox and focus on more important tasks. So let’s dive in and take control of your email notifications!

					function wpturbo_disable_free_purchase_emails($purchase_data) {
    // Check if the purchase is free
    if ($purchase_data['price'] == 0) {
        // Disable the purchase receipt email
        remove_action( 'edd_complete_purchase', 'edd_trigger_purchase_receipt', 10, 3 );
        remove_action( 'edd_send_purchase_receipt', 'edd_trigger_purchase_receipt', 10, 3 );
        
        // Disable the purchase admin notification email
        add_filter( 'edd_purchase_disable_admin_notification', '__return_true' );
    }
}
add_action( 'edd_pre_process_purchase', 'wpturbo_disable_free_purchase_emails' );
				

The code snippet provided aims to solve the problem of disabling email notifications for free purchases made with the Easy Digital Downloads plugin in WordPress. This tutorial will explain step by step how the code works to achieve this.

The first part of the code defines a new function called wpturbo_disable_free_purchase_emails(). This function will be responsible for checking if a purchase is free and disabling the relevant email notifications if true.

Inside the function, we check if the ‘price’ value in the $purchase_data array is equal to 0, indicating that the purchase is free. If it is, we proceed to disable the purchase receipt email and the purchase admin notification email.

To disable the purchase receipt email, we use the remove_action() function with the ‘edd_complete_purchase’ and ‘edd_send_purchase_receipt’ hooks, specifying the ‘edd_trigger_purchase_receipt’ callback function and priority of 10, along with the expected arguments.

remove_action( 'edd_complete_purchase', 'edd_trigger_purchase_receipt', 10, 3 );
remove_action( 'edd_send_purchase_receipt', 'edd_trigger_purchase_receipt', 10, 3 );

By removing these actions, we prevent the emails from being sent when a free purchase is completed.

Next, we disable the purchase admin notification email by adding a filter with the ‘edd_purchase_disable_admin_notification’ hook. The add_filter() function takes the ‘__return_true’ callback function, which simply returns true, indicating that the admin notification email should be disabled.

add_filter( 'edd_purchase_disable_admin_notification', '__return_true' );

This filter disables the purchase admin notification email, ensuring that admins do not receive email notifications for free purchases.

The last step is to hook the wpturbo_disable_free_purchase_emails() function into the ‘edd_pre_process_purchase’ action. This action is fired before the purchase is processed, allowing us to check if the purchase is free and disable the corresponding email notifications.

add_action( 'edd_pre_process_purchase', 'wpturbo_disable_free_purchase_emails' );

By doing this, the function will be executed when a purchase is being processed, enabling the disabling of email notifications for free purchases made with Easy Digital Downloads.

Register an account to save your snippets or go Pro to get more features.