Skip to content

Instantly share code, notes, and snippets.

@damiencarbery
Created December 13, 2023 15:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save damiencarbery/ffeeb245ca532fcde1ce3350c837ac39 to your computer and use it in GitHub Desktop.
Save damiencarbery/ffeeb245ca532fcde1ce3350c837ac39 to your computer and use it in GitHub Desktop.
Add order note after WooCommerce order email sent - Add a private order note when an order email is sent to the customer. https://www.damiencarbery.com/2023/12/add-order-note-when-email-sent-to-customer/
<?php
/*
Plugin Name: Add order note after WooCommerce order email sent
Plugin URI: https://www.damiencarbery.com/2023/12/add-order-note-when-email-sent-to-customer/
Description: Add a private order note when an order email is sent to the customer.
Author: Damien Carbery
Author URI: https://www.damiencarbery.com
Version: 0.1
WC tested to: 8.3.1
*/
defined( 'ABSPATH' ) || exit;
class AddOrderNoteAfterEmailSent {
// Returns an instance of this class.
public static function get_instance() {
if ( null == self::$instance ) {
self::$instance = new self;
}
return self::$instance;
}
// Initialize the plugin variables.
public function __construct() {
$this->init();
}
// Set up WordPress specfic actions.
public function init() {
// Add hook to run directly after email is sent.
add_action( 'woocommerce_email_sent', array( $this, 'add_order_note_after_email' ), 10, 3 );
}
public function add_order_note_after_email( $wp_mail_return, $email_id, $email_obj ) {
if ( !is_object( $email_obj ) ) { return; } // Ensure $email_obj is an object before accessing it.
// Add the note only for emails sent to the customer.
if ( $email_obj->is_customer_email() ) {
$order = new WC_Order( $email_obj->object->get_order_number() );
// Record whether the email was sent okay.
// The date/time are not needed as they will both be automatically shown with the note.
if ( $wp_mail_return ) {
$order_note = sprintf( '<em>%s</em> email sent.', $email_obj->get_title() );
}
else {
$order_note = sprintf( '<em>%s</em> email NOT sent.', $email_obj->get_title() );
}
$order->add_order_note( $order_note );
}
}
}
$AddOrderNoteAfterEmailSent = new AddOrderNoteAfterEmailSent();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment