Shamim Shams
Shamim Shams

Reputation: 181

Woocommerce email on scheduled event

I want to send some woocommerce report to be triggered every night automatically using cron jobs . I have cron job setup for this and I made a custom woocommerce email for this. I am confused how I can hook my email trigger method with my cron event. I have googled on this for a while. But did not get any satisfactory answer. Can anyone please help.

Upvotes: 0

Views: 807

Answers (1)

chrislomas
chrislomas

Reputation: 86

In your custom WooCommerce email you should have an action in the __construct(), something like:

add_action( 'my_custom_email', array( $this, 'trigger' ), 10, 1 );

So then, in the function that is triggered by your wp_scheduled_event(), you add:

do_action( 'my_custom_email', $order_id );

It's worth noting that in your scheduled task function which is triggered by your cron job, you also need:

global $woocommerce;
$mailer = WC()->mailer();

without putting these at the top of your function, the custom e-mail won't send. It took me a long time to figure this out!

So your scheduled task function looks something like:

function my_scheduled_function() {
    global $woocommerce;
    $mailer = WC()->mailer();
    // Lookup $order_id from somewhere
    do_action( 'my_custom_email', $order_id );
}

Hope this helps.

Upvotes: 7

Related Questions