In this guide, I’ll share the process I used to fix the issue with WooCommerce Analytics where the download does not send emails. This involved adding a custom hook to the functions.php
file.
Steps to Fix the Issue:
- Identify the Problem: The problem was that WooCommerce Analytics was not sending an email when a report download was completed.
- Create the Custom Hook: I created a custom function and hooked it into WooCommerce to ensure that an email is sent once the report download is ready.
- Add the Custom Code: The following code was added to the
functions.php
file of the active theme.
Custom Code:
function custom_email_report_download_link($user_id, $export_id, $report_type) {
// Get the percentage of completion for the export
$percent_complete = \Automattic\WooCommerce\Admin\ReportExporter::get_export_percentage_complete($report_type, $export_id);
// Check if the export is complete
// if (100 === $percent_complete) {
// Generate the download URL
$query_args = array(
'action' => \Automattic\WooCommerce\Admin\ReportExporter::DOWNLOAD_EXPORT_ACTION,
'filename' => "wc-{$report_type}-report-export-{$export_id}",
);
$download_url = add_query_arg($query_args, admin_url());
// Use WooCommerce email system to send the email
\WC_Emails::instance();
$email = new \Automattic\WooCommerce\Admin\ReportCSVEmail();
$email->trigger($user_id, $report_type, $download_url);
//}
}
// Hook the custom function
add_action('woocommerce_admin_email_report_download_link', 'custom_email_report_download_link', 10, 3);
Explanation of the Code:
- Custom Function:
custom_email_report_download_link($user_id, $export_id, $report_type)
:- This function retrieves the completion percentage of the export.
- It then uses WooCommerce’s email system to send an email with the download link.
- Hooking the Function:
add_action('woocommerce_admin_email_report_download_link', 'custom_email_report_download_link', 10, 3);
- This hook ensures that the custom function is triggered when the report download link email should be sent.
Testing the Custom Code:
- Add the Code:
- Go to your WordPress dashboard.
- Navigate to
Appearance > Theme Editor
. - Select the
functions.php
file from the right-hand side. - Copy and paste the custom code above into the
functions.php
file. - Click the
Update File
button to save the changes.
- Generate a Report:
- Generate a WooCommerce Analytics report and ensure it reaches 100% completion.
- Check Email:
- Verify that the email with the download link is received.
By following these steps, the issue with WooCommerce Analytics not sending download emails should be resolved. This custom code ensures that users receive an email with the report download link once the report generation is complete.