I need to send two different "new_order" emails depending on the products ordered. The case is that the products are stored at different locations and I need one email to be sent containing only the products present at storage 1, and the other mail containing the products present at storage 2. If only products located at storage 1 was ordered, email 2 should not be sent at all, and vice versa.
The storage information is a custom meta field on each product.
I have added a custom email-class which is a copy of class-wc-email-new-order
but only with changed name where needed.
I've been looking through the hooks and filters for this, but I was not able to go all the way. I've also been thinking about overriding the email-order-details.php
file and check each products meta field, but I am not sure if that is the right way to go. I guess the email would still be sent even if there are no products in the table, so to speak.
My problem is that I don't know which of the emails are sent out when using the different filters, so I cannot check that condition either.
Would you just create two new emails with templates and everything and go that way, or is it something I'm missing here?
If I can clarify anything, please let me know.
Edit: After talking a bit with @helgatheviking I realized that I needed to clearify that the emails regarding storage 1 should be sent only when the order contained products located at storage 1 and emails regarding storage 2 should only be sent when the order contained products located at storage 2. If the order contained both kind of products, both emails should be sent, but only displaying their respective products.
You can customize them like this: https://www.sellwithwp.com/customizing-woocommerce-order-emails/
Var_dump that $order object but I bet you have access to the Product ID which you can then use to figure out which storage it belongs to and adjust the email content accordingly.
After thinking it through and reflecting upon what @helgatheviking said I was able to come up with a solution.
I copied the
class-wc-new-order
and created two new classes exactly the same as the original. I changed ID and class names to storage 1 and storage 2 respectively.I load the classes by doing the following: EDIT WRONG METHOD, SEE BELOW INSTEAD
Notice that this is within a special plugin, you'll have to edit the code a bit if you were to use it in your functions.php.
I then edited the
trigger()
-function in both email classes:Everything seems to be behaving as it should but I'll do some more testing before accepting my own answer. Thanks again to Helgatheviking for leading me onto the right track. :)
Edit: forgot to add my override of
email-order-details.php
. This is added just below<tbody>
The code displaying order items are taken directly from
order-email-items
. I am not sure if there is a better way to do this thought, feel free to correct me if this is the case.EDIT, Skyverge was using an old method to load custom email classes. Correct method for loading custom email classes is: