 All right, welcome back to the channel as I promised you last time that we'll be working on adding another field I will come as a checkout page after we've managed to remove quite a number of things. So Let's jump right in. So today. I want to talk about let's say if we wanted to add Maybe a passport number to our fields here. We're requesting for information So what we're going to do is that we're going to add another field that requests for the passport We'll be able to save that data and we'll be able to reach if it are depending on the different users and We'll also be able to add it to our order field So let's jump right in Alright, so we've already prepared our plugin and we've already added our content that we need to have here and What I'm going to do now is I'm just going to go into my plugins section and actually activate This particular plug-in. So if we just look here, we have it here. So I'll activate it and We have no issues so far and what we're going to do next is now we're going to start tapping into The other hooks of WooCommerce. So we have a hook called WooCommerce before order notes and Basically, what's going to do for us is that it's going to look for Anything that is before this section of adding order notes So this is a hook that we're going to use. So I'll just cut it and then add an action Because we're going to be creating new content. So we use the add action in comparison to add filter which actually Modifies existing content. So this is the hook we'll use and we're going to give it a function And we shall just call it a lab add Passport field so This particular function that we have here. We're going to open up the function Add it and then open our brackets Just copy this here and add it to our function And then the next thing that we're going to do is that we're going to tap the WooCommerce form field Function which is existing in WooCommerce and this will allow us to actually add our content To the particular form field or the processing page Now the few things that we do need is that we shall need an ID for this So I'll just call this the lab Passport, I'll just prefix it to allow it to Pass without any errors and then the next thing that we need to do is that we need to pass an array of information Which will have the type of Input field that it is whether it's a text or it's a number What classes we're going to add in terms of styling what placeholders and so on So in here first of all, we shall add what type of Content it is so we shall add a type which is a text and Then we're going to add a class To allow it to style just like all the other Pieces so we shall add a form dash row dash wide Which we actually have on the checkout form. We shall just look at it quickly So if we go here and then just check out The styling of this field. We shall see that it actually is wrapped in In a span that is wrapped inside a paragraph tag that has a form row and form Row wide as a class So we'll just add the form row wide in there and then we're going to give it a label so the label which we shall call a Passport number For example, and then the next thing we shall do is that if we need a placeholder We can add one so add a placeholder and Then I route that and the placeholder we're going to have is maybe B 0 to 9 to 8 or Something like that. Let's imagine that's a passport number and how we do it and then we shall add whether it's required or Not so is it required if we say it's going to be true. This needs to be a bullion Kind of a post type. So what did we do? Yeah, we forgot a comma here. So I'll save this Okay, the last thing that we need to add to this is that we're going to Add to our checkout. We're going to append the get value Get underscore value Function and in here we're going to assign our ID that we already have here Which is at the lab passport. So I'll just press this and save this and then now of course I'll allow this to be terminated We have a comma here again as we pass information Into this action and into this particular hook. We're actually provided with the checkout We're provided with the checkout argument in our function. So when we pass this in Then we shall be able to actually call it down here. My class should be an array of Content so I'll just make correction there and then save this Yep, and we have it clear. We have our new passport number field actually put in here and It's ready for use. So we have a field already registered and now it's ready to go Now one thing we want to do is that you realize as you go to who come us or if you go into your users account In who come us with who come us installed and activated You'll find that in your profile. You actually have a couple of a little bit of information added to your particular To your particular to your particular profile So what we are going to do is that we're going to also add this information So that it can be picked from your profile should you be a returning customer from time to time So what we're going to do here is that we're going to tap the the wp get current user Function and what we're going to do here is that we're going to store it inside our Variable, so we'll call this our current user So current user is equal to that and what we're going to do is that we're going to get the saved or the Let's call it the saved Passport Number so we're going to get the Passport number that is saved and we shall tap it from the current user and then of course we map that to The idea that we saved here this value here And we shall just add it there and then we shall terminate this with our Semicolon that we're going to add a default a Default field a default attribute and we shall add that as the current Sorry, they saved Passport number that we're getting from the user. So if that is available then we shall make this Available this information available. So if we go back to our field and reload Nothing is broken as we can see. So the next thing that we're going to do is actually we're going to save that information So part one was create the field. The next part is actually to Validate our checkout field. So what we're going to do is that we're going to use the next action hook in our WooCommerce and We shall need a function and the hook that we need here is actually very simple And it's called a WooCommerce checkout process So WooCommerce checkout process That's a hook that we need. So we'll add a function to it and we shall call it a lab validate Passport field I Hope that that is something that makes sense and is Is memorable so we'll add the function close it off and then we'll add of course the function name Which will just copy from here and Paste here and then we'll go inside here and then we shall validate that information. So we start by if adding an if statement and we say if if we haven't posted any information and Of course, this is part of an array and we shall just call This ID if we haven't called Any information in that field and this particular piece is coming from here If I just go here, you'll see that our ID is a lab passport number If we have not posted this information then what we're going to do is that we're going to add a notice and We'll just use the default WooCommerce function which says add a notice and inside the notice we pass in a message will say please add your passport number Of course, we'll add this as an error. The reason we are passing this Okay, I forgot my code semicolon here. I'll save it. No errors anymore The reason we are adding this notice is because we've required this field to be fillable If you come on the front and you'll see that it actually has that red star if we had made this false and Actually save this here and then reload Then there is no point. It's actually saying it is optional So there would be none no point for adding this field here. So I'll just undo this and save So any time we I'll reload this any time we try to place our order and We have not filled in the passport number. It actually tells us Right here in the error, please add your passport number. So I'll just follow the same format of what we have here so I'll just go copy this and then change this here and Say the passport number billing passport Number is a required field. So we'll have some kind of unity coming in there Now the next piece of action that we need to do is that we need to be able to Add this information to our order so that when someone actually fills it out and we're going the back end We should be able to see that order fully filled and then it should have that passport number So what we're going to do is we're going to get the next Action this action is actually Who come us check out who come us check out update order meter. So add it there who come us Check out Update Order meter. So this will add that That meter data on to our order post type So we shall just give this this here and then I'll just call it save new So instead of validate we're going to call it save passport field and in this function function Add the function and it is we're going to get the order ID and Then we shall get these order ID by getting the order and then we shall map get ID function on it and And Before this function of course receives the order the order argument Coming from using this action hook. So we'll get the order in there and we're basically going to use this same information So we'll use this our copy this and say if there is no post Passport number in our in our case if this is not filled in then what we're going to do is that we're going to update our post meter and We're going to update it with a little bit of information. I'll close this off then we can jump right in So the first thing we're going to do is that we're going to first pass the order ID Which is the post ID of the post type order and we've gotten it by just filtering the information here So we add in the order ID The next thing we need to do is that we need to pass in this This field which is a lab passport number But the first thing we do is append it with an underscore at the front And then the next thing that we're going to do is that we are going to pass a sanitized escape attribute Function and then we shall pass in the posted Number, so I'll just get this post Passport number and then pass it in here. So this will come well sanitized and we can Save this. So the next thing that we're going to do is that we're actually going to also throw this as part of our Admin order data, so we're going to throw it in our admin so that we can see it We've finished picking the information through our form here We've validated it and shown that if there is an error then we should be able to Throw a message saying please fill it in now in this instance here We've updated the order meter inside our database. The next thing that we want to do is actually add it on The page so that we can see it as an administrator. So that requires us to use the WooCommerce Admin order Data after Billing address So we're going to add it after the billing address and that's why we use the admin the WooCommerce admin order data After billing address, so I'll just cut this and say add action As our function as our function and then of course the usual We need the hook which will paste in and then next thing we're going to do is just copy this and Then we shall add this and say we shall just call this particular function show admin lab show Admin Passport field in the order so Order that's what we shall call this function and then next thing we're going to do is add the function space Give the function name and then do this and then that what we're going to do is a Essentially the same thing. So I'll just copy this information here Copy it past and then I'll be able to explain what happens. So this also receives the order Because we need to get the order ID. So in this instance, we shall get the order ID and the next bit of information actually here is that We're going to do an if statement But it's going to be getting the post meta. So if Get post meta because we are assuming that this information is there So if it is true if we get the post meta and we pass in the order ID we pass in the The ID of the post meta that we're talking about and then in here we pass that it is true So that it is a string instead of information if that information is available then What we're going to do is that we're going to echo this information and we're going to use some paragraphs so We'll add our paragraph tag which will close and What we're going to do is of course we are going passing some PHP here and The first thing we're going to do is I get the post We're going to get this and Then paste it here. I also realize that I made an error. I didn't close My bracket here. So I'll close that. So this is what we're going to pass in but before we do that I want to show that we actually Are working with the passport number so passport Number we have a strong there then we need to close off the strong tag Here strong Don't forget to put the backslash to close that off. And so we have that echoing out and That's a good step. So we're about to see it on the admin side now the final nail to the coffin of course adding our Our semicolon terminate that is that we might want to pass this information as part of our email That we sent to our customer that this is the information used to book So we have a funk. We have a hook called a wukamas email after Order table So wukamas email after order table So we'll just cut this and we know this is an action so add action As a function, of course it requires its arguments first is the hook But we are pasting in then finally we are adding a function name And the function name will be Show Show passport field emails. So we'll just copy this lab show passport field emails So then I'll terminate this and then we'll just copy what we have here As a function So function terminate that as us and then inside our function What we're going to do is essentially the same thing that we did here So if in our order, we have this post meter of a lab post number if it is true Then we're going to get a Couple of things that we do need here. Of course in the post meter We're getting order ID, which is a post ID then we add in the ID or the name of The meter that we want to get we add in true so that it comes back as a string and then finally We're going to echo it out and say This is the same thing that we do need now. Of course in this here we In our function as we pass This particular hook we get the order just jumping in there. So what I'm going to do is I just get this same thing here Copy and store our order ID in that way So that when we're querying for our post meter We directly just add in the order ID and then when we're of course throwing it here We also still get the same thing now, of course in this This particular function we get a number of other arguments that are thrown in so sent to admin So it's being sent to the administrator And then we are sending it as a plain text And then we are going to send it to the email of our customer So this is the information that we are getting and we are throwing and passing in Into our email. So I'm going to save this when we come back here and add our Our passport number in there and then place an order We actually get a server in an internal server error because we have an error here In the file quickly browse through this. I'll actually see the hook The wuka must check out update order field Actually passes an order ID not order So we'll just add order ID here And then of course that will work So let me just clear this out because we forgot the not here when we're copying that's why it's giving us an error Now, of course when we add these other arguments into our Function we need to tell WordPress that we actually are going we first of all need to give it another priority and say maybe let this come at 20 But we're going to be passing in four arguments So to remove any errors we need to pass that in our Our our action hook. So we now come back to our shop add some orders into our cart Then go to view the cart Skip updating. Let's go to process the checkout All our information is coming back because that was already added So when you add our passport number for example and say this is a nice order for me And then place the order wuka must checks out and then we have Our order is saved here. So what I'm going to do is go back on the back end And look at the orders and we'll see that this is the last order that we made of 73 pounds So when I check it in here, we're going to see that we have our passport number actually highlighted here in our order details and We are about to find this information useful here Now, of course if I was to check my email, of course, it's a local server. It's not well set up for mail But I would also see that my email would come the passport number actually added in there as the content So thank you for checking out this video. I hope this is helpful that you'll be able to customize your orders even better So let me know if you have any other queries or any questions And please leave a comment like the video or subscribe to the channel if you haven't yet Thank you for watching and happy coding and enjoy your e-commerce treat