From your description and the provided screenshots, it's challenging to pinpoint the exact cause of your problem, because the issue can result from multiple possible issues. To effectively debug this, additional information would be helpful, such as:
The exact file structure (specifically, the paths where your email templates and CSS files reside).
The code inside your custom email classes (class-wc-email-*.php
).
Any specific error messages from WooCommerce debug logs. (in wp-config.php), so we can see debug logs in /wp-content/debug.log, or for woocommerce /wp-content/uploads/wc-logs.
define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
However there are multiple problems that I can see in your code:
$emails['WC_Email_Customer_Doi_Xac_Nhan_Order'] = include get_stylesheet_directory() . '/woocommerce/emails/class-wc-email-customer-doi-xac-nhan-order.php';
This approach is incorrect because you're directly using include
, which returns only a boolean (true
) or the return statement from the included fileānot the actual instantiated object you need for WooCommerce emails.
I would use it like this:
add_filter('woocommerce_email_classes', 'add_custom_order_status_emails');
function add_custom_order_status_emails($emails) {
// Include your email class files
require_once get_stylesheet_directory() . '/woocommerce/emails/class-wc-email-customer-doi-xac-nhan-order.php';
require_once get_stylesheet_directory() . '/woocommerce/emails/class-wc-email-admin-da-cap-nhat.php';
require_once get_stylesheet_directory() . '/woocommerce/emails/class-wc-email-customer-da-cap-nhat.php';
// Properly instantiate each email class
$emails['WC_Email_Customer_Doi_Xac_Nhan_Order'] = new WC_Email_Customer_Doi_Xac_Nhan_Order();
$emails['WC_Email_Admin_Updated'] = new WC_Email_Admin_Updated();
$emails['WC_Email_Customer_Updated'] = new WC_Email_Customer_Updated();
return $emails;
}
// Trigger custom emails on order status change
add_action('woocommerce_order_status_changed', 'trigger_custom_order_email', 10, 4);
function trigger_custom_order_email($order_id, $old_status, $new_status, $order) {
if ($new_status === 'doi-xac-nhan') {
WC()->mailer()->emails['WC_Email_Customer_Doi_Xac_Nhan_Order']->trigger($order_id);
}
if ($new_status === 'da-cap-nhat') {
WC()->mailer()->emails['WC_Email_Admin_Updated']->trigger($order_id);
WC()->mailer()->emails['WC_Email_Customer_Updated']->trigger($order_id);
}
}
WooCommerce expects e-mail specific CSS to be available in the following files:
your-theme-folder/woocommerce/emails/email-styles.php
Before doing a real test, you can test it in WooCommerce -> Settings -> Emails -> [Your Custom Email] -> View Template
Please let me know how you progress - and if you provide more information, it will be handled easily.