Whenever you want to export something from WordPress, there are a handful of plugins including one of my favorite WP All Export by Soflyy. This plugin is very powerful and can export almost anything from your WordPress site. However, not all functionality depending on your needs is built-in so that’s why they offer you a lot of action and filter hooks including the feature to write custom PHP functions and using then while processing the data.
Recently, I had a task to export WooCommerce orders in CSV format. So the requirement was to export complete order data including customers first and last name in the single CSV column.
WP All Export offers separate fields for both billing first and last names, but not a single one for full name. So our solution should get first and last names, combine them and return the full name.
To build this solution, we will write custom PHP function that accepts order ID and performs following two things:
- Checks if the customer is a registered user. If yes, then grab customer’s first and last name from the database and return the full name.
- Else, if the customer is a guest user, grab the “Billing First Name” and “Billing Last Name” and return the full name.
To start, first copy the following code into WordPress Admin > All Export > Settings > Function Editor:
Then, you have to use this function in the WP All Export export file template. Now, I am skipping to the part where you are on the Customize Export File screen
Now, follow these instructions:
- Add a new field from the right sidebar
- Edit the field by clicking on it, fill out the information as in shown in the screenshot below:
- Now click on “Done” and you’re ready.
That’s it. Now the export file will contain the full name of the customer in a single file. Doesn’t matter if the customer is registered or a guest user, it will always contain their full name given either in the customer profile or the order billing details.
If you have any questions, leave a comment below. I would love to answer. 🙂