WordPress is the most common web deployment cms platform. It expedites user experience when making a website for a business. Substantially, it offers drag and drop interface, data entry, and plugins to manage the website structure. It comes up with the “Woo-commerce” plugin to create a store on your website with all basic functionalities like product listing, inventories, creating or receiving an order, etc. But in some cases, you may need to go beyond it for particular requirements. Therefore, “Wordpress Woocommerce also provides a “Legacy API”, an API that you can programmatically setup WordPress API into your code or script to orchestrate products and customers’ data. However, newbies may expect the exertion when working around WordPress woo-commerce legacy API.
Setup WordPress API
In matters of this kind, setting or installing woo-commerce legacy API at cpanel using PHP script is a very general issue. In this guide, we are about to guide you that How you can set up WordPress API at cpanel using PHP programming to retrieve order and customer details.
Step-01: (Install Legacy API Libraries under your website domain)
Login to your cpanel, search or navigate for SSH Terminal. Open it. Note: It’s sensitive, follow carefully, a wrong move (use of incorrect/unknown command) may put your server down.
By default, this terminal opens up with the root directory. Using this terminal, we need to approach to website folder/directory where our WordPress website exists. Mostly, website folders/directories contains in the “Public Html” folder. So let’s assume the website is elambak.com which is located in a public_html folder/directory.
So we’re heading towards “Public Html -> elambak.com” to install woo commerce API under “elambak.com”. Use the following commands (highlighted) to approach the website folder/directory for www.elambak.com.
Commands:
Cd public_html Press Enter (This command will open the public_html folder).
If you are not sure whether your website contains in the public_html or not. Then run the next command to see what’s inside the public_html folder/directory.
ls Press Enter (LS means list, this command will list all folders/directories for the websites which are part of public_html)
Now, we can see our website folder/directory is listed under public_html, so in the next command, we will move from public_html to our website folder/directory.
cd elambak.com/ Press Enter
We have approached our website folder/directory for elambak.com. Now we can run the command to install woo commerce API libraries in it. Run the following:
composer require automattic/woocommerce Press Enter
Now, you can disconnect and close this terminal.
Step-02: (Setup API Connection between WordPress website and your script)
We need to set up a connection between the WordPress website and the PHP script that will create. Follow the following instructions from the WordPress admin panel.
- From the WordPress admin panel navigate to “Woocommerce -> Settings”
- Then click the “Advanced” tab, under the options from the Advanced tab, click “Legacy API” and enable it.
- Now, Click “REST API” from the same place (under woo commerce setting page)
- Click “Create an API KEY”.
- Give a title to this API, and click “Generate Keys”.
Once it’s created, two keys will generate that you need to copy and we will use both keys in our PHP scripts.
Sample PHP script to initiate the API.
Open cpanel file manager, navigate and open the website folder/directory. Create a PHP script in your website folder/directory, name it anything with an extension of “.php” and save this file. (Note: The website should be the same as where you installed API in the previous step. In this guide, we installed API in elambak.com so we’re creating a PHP script in the same website folder/directory.).
Just copy and paste the sample code from the snippets above for API initializing, and replace the following values:
- Website domain
- Consumer key (Starts from ck)
- Consumer secret (Starts from cs)
Now, we need an order id from WordPress for which we want to retrieve details. You can take any order id from WordPress for testing.
Add the following code to print all information for order in JSON format is:
print_r($woocommerce->get('orders/45645'));
Where print_r will print the result, $woocommerce that we initialized for API, and 45645 is the order id.
Visit the PHP script page on your browser (on which we’ve called this API). This script page will show all data for the given order id in JSON format. If you want to get specific data like the “First Name” of the customer. Usually, the first name of the customer remains the part of “billing” section. But you can also check where the first name contains in the JSON result.
To print all data regarding the billing replace the code:
print_r($woocommerce->get('orders/45645')); //replace it
Replace the above line of code with this new code:
$order=$woocommerce->get('orders/45645'); //new code
Now instead of printing, we’re storing order details in a separate variable named $order
Add a line to print just billing information:
print_r($order->billing); //code to print only billing details
Visit/Refresh the PHP script page again at browse, this time it’ll show only billing data for the given order id. Now, to print just the “First Name” of a customer from billing information replace the code:
print_r($order->billing); //replace it
Replace the above line of code with this new code:
echo 'First Name Is: '.$order->billing->first_name; //new code to print name
Note: In the API result for billing information the “First Name” was assigned with the key “first_name” so it should be the same as first_name in the code as echo ‘ First Name is: ‘.$order->billing->first_name;
Visit/Refresh the PHP script page again at browse, this time it’ll show only “First Name” for the given order id.
Video Demonstration: