Join Patrick Rauland for an in-depth discussion in this video Sending automated updates to customers, part of WordPress Ecommerce: WooCommerce Plugins.
- [Narrator] Creating a custom order status is really good for your company's workflow. But you still need to let customers know about your process. You probably want to email them and tell them that your in your build phase, and that they can expect another email a week from now and their items to ship, shortly after. Let's code that. We can programmatically send the customers an email when their order moves into a specific phase. We'll go to the code, in our building status plugin. We'll scroll down to line 23, add new line, and there's a handy action called 'woocommerce_order_status_changed' and then we can run one of our own things, after that.
We'll say, array, is in this object, and we're going to call it 'building_status_message'. Now normally, we're totally fine with the default priority of 10, but in this case, we really don't want someone jumping in here before us, and we especially don't want the page to redirect before we get to execute our code. So we're going to set the priority to one and we also need to send through three parameters.
Add the semi-colon and let's scroll down, past all this, and at line 76, we'll add a few new lines. Let's create that function, public function building_status_message and we're going to add those three parameters. The first one is the order_id. The second is the old_status and the third is the new_status.
We're going to use WooCommerce mailer functionality later, so I'm going to access the global WooCommerce variable right now. Now we want to make sure, that our code only runs when we change the status to building. We don't want it to run when it changes the status to completed, or something else. So we're going to say, if, something is set to 'building', and in this the case, that something being the new_status, then we want to run all of our code, right here.
One thing to note, earlier, way up here, we created a status of 'wc-building', you don't need the wc- when you're checking it down here. You just need the thing that comes after the wc-. We're going to need the order object, in just a minute, so I'm going to go ahead and create it, right now. The order object will let us get all sort of things about the order, like the order number, or the billing email address, things like that.
So I'm goin to type in $order=new WC_Order($), and we're going to pass in that order ID Now we could use the default wp_mail function, which does send an email. It's just that the email is pretty plain and doesn't really have any branding. I'd rather use the mail functionality within WooCommerce. This is something that I worked on awhile ago and it's a much prettier email message. It uses your brand colors and it'll match any other WooCommerce email you send.
Now I'm going to copy and paste this for speed. So I'll copy that and right here, after we get the order, I will paste that in. I'll go ahead and indent it. So what we're doing here is we're getting the subject, we're loading the mailer, and then we're sending it, with a whole bunch of functionality going on here. I'm going to pull out a little bit of this, process it up here, and then send it. So first we're going to change the subject to 'Your Order is being Built!', this way the customers know, even just from the subject, that it's under construction.
Next I'm going to add the message and I'm going to call this message_body. We'll set it to something like 'Hey there! We're working on your order. It should be built in a week and then you'll receive an email from us.' Now I used single quotes here, which has created some problems, so I'm going to wrap this in a double quote and then I'm going to remove that single quote.
That's a little bit better. Don't forget the semicolon. And then we're going to take a look at this email because we still need need to somehow add a header message right up here, while the rest of our message is going to go down here. So let's go back here. We're going to use this mailer wrap function. So I'm going to copy this and pull it out and I'll get rid of these extra quotes here, and up here I'm going to do something with this wrap function.
So I'm going to create a new variable called message, this time, and we're going to wrap our message body with that wrap function. So I'm going to set this equal to mailer, followed by wrap_message, and then we're going to put the subject, which is right here, at the top of that email. And then for the message, I'm going to copy our message body and put it in right here. So now we have the subject going right here and the message body going right here.
Now because of the way we're doing all of this, we need to be careful when we load the mailer. Notice that we're loading the mailer down here and we're using it up here. This makes sense to me to keep all of this HTML formatting up here, so let's just load the mailer a little bit earlier. I'll add a comment and let's not forget to actually send that message. So we have the mailer, which is sending, we're sending it to the admin, which is something we'll change, with the subject, the email subject, and the message.
For the last thing here, we want to change this from the admin_email to the billing email. We're going to use that order object that we created earlier. So $order->billing_email. I'm going to correct this typo up here, can't have those, and then we'll save this. Before we switch over, I can't forget, a semicolon, right here. Alright, let's save that and now let's check everything out. I'll go to this email, which is Processing, and I'll change it to Building.
There we go, it's been updated and the order status is set to Building. It changed from Processing to Building, right now. And if we go back to the admin, we can see that they're both in the building phase now. Awesome. So an email should have been sent to our customer. Now whenever you're working with email make sure that you have some sort of third-party, SMTP provider. Mailgun is a good example of that. Your web server isn't necessarily very good at sending email and sometimes they get caught in spam filters.
So if you didn't get your email, make sure you use a service like this, that sends your mail for you.
- Creating a WooCommerce plugin
- Finding hooks within WooCommerce
- Creating settings pages
- Analyzing WooCommerce settings
- Configuring settings
- Customizing the edit product page
- Using hooks on the product page
- Customizing the WooCommerce checkout
- Adding custom order statuses
Skill Level Intermediate
Q: This course was updated on 08/07/2018. What changed?
A: The following topics were updated: adding a meta box, saving product fields, customizing product titles, and adding data to product page tabs.