This code snippet modifies the subject line of completed order emails in WooCommerce specifically for downloadable products (such as eBooks). By checking the product category, it ensures that the email subject is relevant and informative for the customer.
Add the code to your child theme’s functions.php file or through the Code Snippets plugin.
// Customize completed order email subject for downloadable products
add_filter('woocommerce_email_subject_customer_completed_order', 'custom_completed_email_subject', 10, 2);
function custom_completed_email_subject($subject, $order) {
$category_id = 43; // ID for the eBook category
$has_downloadable = false; // Flag to check if order contains downloadable products
// Loop through each item in the order to check for eBook category
foreach ($order->get_items() as $item) {
$product = $item->get_product(); // Get the product object
// Check if the product belongs to the eBook category
if ($product && has_term($category_id, 'product_cat', $product->get_id())) {
$has_downloadable = true; // Set flag if eBook is found
break; // Exit loop early if we found an eBook
}
}
// Modify the subject if the order contains downloadable products
if ($has_downloadable) {
$subject = 'Your eBook and the invoice for your order ' . $order->get_order_number(); // Set new subject
}
return $subject;
}
Leave a Reply