WooCommerce: Send tracking info to customer using custom meta fields
This post covers the creation of a WooCommerce custom order action.
The action will send a notification to our customer with a predefined message.
For our use case, a shipping tracking info message will be send along with a tracking number.
Adding custom order action
This step will show a new action under the "Order actions" dropdown.
Edit your theme's function.php
with the following snippet:
This won't do anything for the time being.
We will define what the action does on the next step.
Executing the custom order action
The following snippet checks wether a meta field exists in this order. If it exists and it is not empty, it will send a notification to the customer using the add_order_note
command.
Note
Notice the
1
in$order->add_order_note( $message, 1);
.
If we remove the1
($order->add_order_note( $message );
) our text will be saved as a note and won't be sent as a notification to the customer.
Adding the custom meta field on new orders automatically
The following snippet will add an empty meta field to the order during checkout.
Sending the notification
While you are on the order page you may scroll down to find the custom fields box.
If an order has been placed after you inserted the above snippets, a my_shipping_tracking
should be shown there with an empty value.
You may update that field's value with the tracking number your shipping company has given you.
If the order was placed earlier, you won't find the my_shipping_tracking
field and you will have to create one manually.
Once you have that field updated, find the Order actions dropdown, select "Send tracking info" and press the execution button.
When the page updates you should see a new note added under the Order notes section. This note has also been sent to the customer.