Reputation: 377
I want to accomplish the following:
With the Woocomerce Completed Order Email a pdf is generated and sent out as attachment. After the Email is sent, the pdf is deleted on the server.
I achieved the first part with the woocommerce_email_attachments Filter like this.
add_filter('woocommerce_email_attachments', 'attach_ticket_pdf_to_email', 10, 3);
Now for security reasons I want to delete the pdf that was generated on the server AFTER the Email is sent out.
I found the Action 'woocommerce_order_status_completed’ that could be hooked into, but this is not called if I resend the Completed Order Email from the admin backend.
Is there some hook or filter or action that is called to send out the Emails in Woocomerce. Or after the emails are sent?
There I would like to call a function that deletes those pdfs again.
Any ideas?
Upvotes: 1
Views: 988
Reputation: 26319
The emails are triggered with a default priority of 10. Therefore, I presume that you could call your function with a higher/later priority and it would be fired after the email was sent.
If you re-send an order email from the admin you can use the woocommerce_after_resend_order_email
hook. This gets passed an $order
object so you can't quite attach the exact same function to both.
Here's how I would start:
add_action( 'woocommerce_order_status_completed', 'delete_pdf', 20 );
function delete_pdf( $order_id ){
// do your thing to delete the file
}
add_action( 'woocommerce_after_resend_order_email', 'after_resend', 10, 2 );
function after_resend( $order, $action ){
if( $action == 'customer_completed_order' ){
delete_pdf( $order->id );
}
}
Upvotes: 1