Delay the sending of emails from the Advanced Notification plugin.
Over 3 years ago I wrote a plugin to delay the sending of WooCommerce order emails. While it has helped a few people, other people have been left frustrated because some plugins do not use the WooCommerce email functionality. Recently Solei asked about deferring emails from WooCommerce Advanced Notifications, a premium plugin. The question coincided with some free time so I investigated. And found a solution.
Simpler than the original plugin
When I first looked at the WooCommerce Advanced Notifications code it wasn’t obvious how to prevent the emails from being sent and how to then send them later. It was using custom actions to run functions which checked order details and plugin settings. While investigating I even went as far as revisiting 6 year old code to examine the functions attached to one of the hooks, looking for the plugin’s function there (and wrote a simple bit of code to search for a class and function – I’ll expand and publish that some day).
I was thinking that I would have to copy some of the plugin’s functionality into my plugin. This would be a terrible idea as my plugin would have to change if the Advanced Notifications plugin changed. This is definitely the opposite of how I like to code – I prefer to use an API (and not hacks) to keep my code as future proof as possible.
After a good sleep I decided to try a simple approach – remove_action() the original calls and call the plugin’s functions later. It was fortunate that plugin instance was stored in the $_GLOBALS array so I could easily call its member functions. This approach worked and the code is shorter than the code in the original post.
The code
The code below defers the purchases emails (order based ones), not the stock ones (low stock, no stock) but I can add those if someone needs them. It is a MVP (Minimum Viable Product). If there are issues I will be happy to look into them.
Leave a Reply