 Welcome back to the channel. In this video, I'm going to show you how to send SMSs to your customers from WooCommerce when they make a new order or when you go inside your administrative area and add a new order note. So if this is something you're interested in, then get ready for the code. Let's dive in. When a customer comes to your shop and they make an order for a product and then they scroll the way, choose to order after filling in their details. Your order is received here and there are two things that do happen. One is that if you go to that order in your administrative area, you'll see that there's an order that's placed there and when you click on it, there are order notes that are created. So the payment to be made upon delivery and then the order status changes from pending to payment to processing. There are a number of things that are happening. This is what we call an order note and it's created every time your order changes its status. And as the shop owner, you can choose to have a note that you send to the customer. Maybe we had an issue with, we had an issue with the shipping and your verse broke. Let's say you have bad news, you could write a note to your customer and then when you click add, your customer received an email with this particular note. So let me check my email. You see that in my email, I got the order email and I have now received a note coming in telling me that there was a problem with my order. So there was an issue with the shipping and my verse broke. So those are the two situations that we have in WooCommerce that trigger a notification to the client. Now, the purpose of this video is for me to tap into those two triggers where we have the order note and the order status changing from WooCommerce and using that to send an SMS to the client. The reason why we would do that is that SMS is a more instant to a customer and they are more personal. They let a customer know before they even check the email that there is something going on with their order. So let's jump into the code to see how we would do that. I'm now into my root folder of my website. I go to WP content into the plug-in section. Then I'm opening up a new plugin which I'm calling Techie Press SMS Notifier and inside it, I'll create another file called TechiePressSMSNotifier.php and once I click this to edit it, I have this new file that I have prepared to have this code to begin off my particular plugin. So with this, I am ready to trigger the plugin to start. So I'll go back to my admin, go to my plugin section and then I'm going to activate TechiePressSMSNotifier. So I'll activate this and now any code that we shall write here will run because the plugin is actually active. Let's start off with the actions that will tap into the two events that we talked about. One, the event of a new email for the order and that order comes from a change in status. And then secondly, I want to tap into the event of the order not. So those are two distinct situations that I'm going to use to tap in and actually send a particular SMS to my mobile phone. So let's start with those. So we'll start off with add action and we're going to tap into the WooCommerce action, which is called WooCommerce order status change. And it's the one that we have right here. And with this, I'm going to jump in and send SMS on new order status because we might want to keep informing our client that the order has changed from the paying stage. It's now shipped. It's now delivered and it's now completed. We should be able to notify our customer at all those different stages. So we'll use this as a callback function to actually write that. So I'll write this function here, open it and right now it's not going to do anything, but it will allow our code not to break. So we're going to add another action and in here we're going to tap into the order not so that whenever they are written, we can be able to send another SMS. So I'll copy this function here, duplicate it and we'll call this send SMS on new order not. Now we know this don't seem to be very, very unique names. So I'm going to just add on take a press at the beginning of each to allow a little uniqueness to come in so that I don't have any other plugins breaking because of me not doing any due diligence. So we'll copy this function, which is for the order not added here. The action that we'll use is actually called WooCommerce new customer not notification. So with this, we shall be able to jump into the code that will pick up this particular pieces. Now let's start off with the new order status. If you look up this particular action of WooCommerce, you'll find that it actually comes with arguments that you can pass into your function right here. So that's why I'll write this to have a priority of 10 so that it's early in the stages of the order status changing. And I want to tap into those four different arguments. And these arguments are I'll just pass them on down here and we'll look at each one of them. We have the order ID, we have the old status of the order, we have the new status, and we have the order itself. Now each one of these comes with different data that we can tap into. But we'll use this when the time comes for writing this function. And equally, this action for the new customer notification also has its own arguments that you can pass inside this function. And that argument is actually called email ads. Email ads comes with an array of data that we shall use to tap into and write our own function down here. For now, let's just stick with the first action of sending whenever the order status has changed. So the first thing that we're going to do is actually get the order object. Now the order object will have a lot of information that we can use in our function. I'll give this variable my order as a name, and I'll go for the wc get order, which is the function in WooCommerce. And inside this, I'm going to pass in the order ID that we get from here. So I'll add the order ID and then we'll be able to get an order object down below. Now where are we getting the order? Now if you look inside an order in your administrative area, you'll have a lot of information like the first name, the billing address, the email address, you have the phone number, and that's information that the customer has given to us deliberately. Now all we need to do is just tap into this and use it forward. That's the reason why we actually have to get the order here and get the order object. Then we can tap the first name and all the other information that is available. And how do we do that? Now we're going to get our first name and we're going to get that from using the order object that we have here. So we'll say my order, and we're going to use a method, we're going to chain on a method from who come as which is gets, billing, first name. And so we'll have our first name coming in from our order. And then the next thing that we're going to do is I'm going to duplicate this so that we can now also get the phone number because that's where we'll be smsing and we need to just get get billing phone from that order object. Maybe on our checkout we had specifically put another field. Then we'd need to get that field, which is in most cases served as post meta, and then we would add it here. So let me just pick these comments and add them on the side so that I can be able to quickly read everything else. I need to change this into phone so that it's distinct. So we have our first name, we have our phone. What else might we need from the order object might need to get the shop name. And we're going to get this from this toward details inside the database. And we tap into the database by getting the option. And the option that we're going to look for is actually called who come as email from name. And this will give you the shop name that you store when you are setting up your particular who come as install. So we'll be able to get the name from this. Now let's go back into our code. We are right on time to start setting up our default default SMS message. And we shall say that is going to equal to we shall put this in double brackets because we might want to escape a couple of things. So I'll say thank you. And then I'll add first name. Thank you first name for shopping with shop name. Then the next thing that we're going to do is say your order. And then we shall add the order ID in this case order ID. So we'll be having something like your order. Then the pound sign and then the order ID is and then we shall add the new status of our order is maybe pending payment pending is shipping or whatever. So we have a default message and we are now good to go. Now depending on how what API we're using to send our SMS, we're going to now write a function that we shall break out of this code because we're going to use it here and also use it in also this other function down here. So we don't want to duplicate our code. We are planning ahead of time so that we can save on particular resources on our server and also in our code if we need to make any changes. So I'll add a function which I'll say send SMS to customer. I'll prefix it with take a press so that it's unique to the different users. So I'll copy and paste this in here but comment it out because we're not going to use it just now and then I'll start writing that function down here. So inside this take a press send customer, we're going to pass in a number of things. For example, we shall pass in the default message because we want that message to be sent and we're going to pass in the phone number. We're going to require the phone number that will be sending our message and in most cases, the different providers of SMS that we'll be working with might require to have an ID of the person sending the particular SMS. So it would be good for us to just use the shop name. So I'll pass in the phone, the default message and the shop name. So once this is done, we shall be able to tap this particular values and also pass them inside our take a press send customer. So now we're going to focus on the API where we're going to be posting or sending our messages at this particular time. So let's now start writing that code. However, if you want to get the nitty-gritty of it in this video description, I have added a video where I work with posting SMS to another API, any kind of API using WordPress. Now I'm going to type into my phone number for it to be null. And the reason why I want to do that is if I set it by default to be null, I can choose to tell my code. If the phone number is still null, if we don't get a phone number back from here, then don't bother sending the message because that will break. I don't need to do any of that to waste my resources. I am pasting some code down here that I'm going to go through line by line so that you can understand what each one of them is actually doing. Now I have my API on this URL, which I'm going to cut from here and just paste it here so that you know that I am working with this URL for the API. And I'm going to choose to add some code here that says if in brackets, if our phone number is actually equal to null, if it's equal to null, but WordPress works in the order format, so we shall say null equal to and I'll change this. If our phone number is equal to null, then we shouldn't bother doing anything. We should just return and not process the code that is down below. However, if our phone passed in here from this function here is actually with something that's useful, then this particular code down below will run. So we have our URL here for our API. And then we have this structure that our API expects from us. So our API expects to have an array of data that has a method of send SMS that has user data where we have the username and the password and then we need to have the message data and the message data is going to be an array with another array and inside here we shall have the phone number. So we're just going to copy this phone and we're going to pass it in here and then we shall have our default message. I'll copy this and send it here. And then our sender ID, which we are, which we shall copy here and pass in as the phone name. So we have a proper array of data. So I've put in the username and the password that we need to complete our MSG data variable. Now, the next thing that we shall do is use the WP remote post, which is a WordPress function that requires a URL, which we have here for our API. And we're going to be posting data. So that's why we use the remote post, the WP remote post function. And it requires arguments. And these arguments are the ones that have the method you're using. In our case, we shall be posting. So the method is post. And that's where we also pass the body in here. So I'll undo the comments here so that we have our arguments accessible inside our WP remote post function. Now, you'll look at our body and you'll see that we are encoding this variable, MSG data, what we have here, the array of data with more arrays inside it. We're encoding it into JSON that will be usable and acceptable by this particular API where we're posting the data. Now, after posting our data to our API, we shall record whether we have an error by using the is WP error function here, or we'll allow it to just pass by and let us know that it has successfully made a message. So I've realized I've made an error here. So this should be in quotes, then now, and then I'll save this. And then I'll come back to our order. And I'm going to change its status from pending payment to processing and I'll click update. And we get an email on our phone. We have our SMS telling us what's happened. So there are just a few things that we need to clear up in our SMS because I'm not getting the order number, but everything is working out well. So it means we can actually take this a level further and just throw this inside with our customer notification. So I'm going to undo this. And then I am also going to undo our function here. I'll copy this, what we have right here and paste it in here so that we can change only a few things as we go along the way. So for us to just get this note that is showing up here, we just need to go back into our code and we're going to tap the email ads in here. So from our email ads here, we're actually going to get our order so that we get the same details. So we get our order. It's going to equal to wc underscore get order. And that's a function that's coming from WooCommerce. And in here, we're going to pass in our order ads. And from that array, we shall be looking for the order underscore ID. After getting our order, the next thing we need to do is get our note. And the note will be equal to the email ads that we have here, email underscore ads. And we're going to look for the customer note inside this. So got customer underscore note. And once we have that, we're now able to change our default message. So I'm going to get the note from here and then replace it with the default message. And then I'll get rid of this. So we can utilize the same variables from here and then come down here and paste them. So after copying those, I'm going to just change this to order so that it matches our order here. And what I'm going to do now is actually save the changes. After saving the changes, I'll go back into our admin. And what I'm going to do here is I have typed out this little note that I'm going to send to the customer instead of a private note that just remains for the shop manager. I'll send it to the customer. And then I'll click add. And once I click add, you'll see that our order not gets stored here. And we get our SMS showing up on the phone. So it's working as we desired. And now the only thing that you need to do is change all these things that need to be dynamic and we'll put them into an internal interface. Let's say our own page here or a page that we can add to our commerce inside the settings so that this becomes something that we manage on the admin area. And we don't have to add inside the code. But that's how you set up a system that will allow you to SMS your clients whenever you change the status or when you add a new custom note notification. I want you to note that a new order status when someone makes a new order, the status is changing. So definitely you'll get a message talking about a new order being made. The other thing that could change is how your API works. So you need to know how your API works and you implement that inside this particular code. And if you want some help on that, please watch the video that I've linked in the video description right below. And you'll see how to work with any API and how to send data to it from the basics all the way to you doing all that you need. Thank you for watching the video. If you enjoyed it, give it a thumbs up, share it with your friends. Don't forget to subscribe to the channel if you've not yet done so. So enjoy your day and peace.