In this series, we're discussing the REST APIs in OpenCart. In the first part, we went through the setup of API user credentials from the back-end. In this part, we'll extend it and go through the rest of the examples, showing how to set shipping, payment and customer related data in the cart. Finally, we'll conclude the article by placing an order!
If you haven't gone through the first part yet, I would recommend that you go through it. Let's have a quick recap of what we've done so far in this series.
- We created API user credentials from the back-end.
- We set up the common file which is used in all the examples to make curl calls.
- We went through the API usage by providing examples of "How to log in", "How to add a product in the cart", and "How to edit a product in the cart".
Today, we'll extend our journey and see a couple more examples which we'll need to create a complete order in OpenCart using the API. We'll start from where we left the last part, so I assume that you've created the common.php
, login.php
, add_product.php
and edit_product.php
files already.
How to Add Shipping Information
To start with, we'll first add the shipping address.
How to Add a Shipping Address
Create a file add_shipping_address.php
with the following contents.
<?php require "common.php"; // set up params $url = 'http://your-opencart-store-url/index.php?route=api/shipping/address'; $fields = array( 'firstname' => 'Sajal', 'lastname' => 'Soni', 'address_1' => 'Abc Street, 105', 'city' => 'Ahmedabad', 'country_id' => '99', 'zone_id' => '1485', ); $json = do_curl_request($url, $fields); $data = json_decode($json); var_dump($data);
We're passing all the required fields for the shipping address in the $fields
array. You should see a "Success: Shipping address has been set!" message in case of success!
How to Add Shipping Method
Create a file add_shipping_method.php
with the following contents.
<?php require "common.php"; // get list of shipping methods $url = 'http://your-opencart-store-url/index.php?route=api/shipping/methods'; $json = do_curl_request($url); $ship_methods_data = json_decode($json); // fetch "code" of the shipping method we want to add $ship_code = $ship_methods_data->shipping_methods->free->quote->free->code; // set up shipping method $url = 'http://your-opencart-store-url/index.php?route=api/shipping/method'; $fields = array( 'shipping_method' => $ship_code // 'free.free' ); $json = do_curl_request($url, $fields); $data = json_decode($json); var_dump($data);
To set the shipping method of the order, we should know which shipping methods are available in the first place. Thus, we've retrieved the list of shipping methods in the first CURL call in above example. I assume that you've enabled the "Free Shipping" method in the back-end, as we're going to use it as our shipping method.
In the next CURL call, we've passed the shipping_method
code as an argument which is required to set the shipping method.
Finally, you should see "Success: Shipping method has been set!" method as a success. One important thing to note here is that the order in which you set "Shipping Address" and "Shipping Method" is important. First, you'll need to set the "Shipping Address" and after that you should make a call to set "Shipping Method".
How to Add Payment Information
How to Add a Payment Address
Create a file add_payment_address.php
with the following contents.
<?php require "common.php"; // set up params $url = 'http://your-opencart-store-url/index.php?route=api/payment/address'; $fields = array( 'firstname' => 'Sajal', 'lastname' => 'Soni', 'address_1' => 'Abc Street, 105', 'city' => 'Ahmedabad', 'country_id' => '99', 'zone_id' => '1485', ); $json = do_curl_request($url, $fields); $data = json_decode($json); var_dump($data);
This is almost identical to the "Shipping Address" example, except that it'll set the payment address of the order. You should see "Success: Payment address has been set!" in the case of success.
How to Add a Payment Method
Create a file add_payment_method.php
with the following contents.
<?php require "common.php"; // get list of payment methods $url = 'http://your-opencart-store-url/index.php?route=api/payment/methods'; $json = do_curl_request($url); $payment_methods_data = json_decode($json); // fetch "code" of the payment method we want to add $payment_code = $payment_methods_data->payment_methods->cod->code; // set up payment method $url = 'http://your-opencart-store-url/index.php?route=api/payment/method'; $fields = array( 'payment_method' => $payment_code // 'cod' ); $json = do_curl_request($url, $fields); $data = json_decode($json); var_dump($data);
Again, pretty similar stuff as we did for the "Shipping Method" example. In the first CURL call, we've retrieved the list of payment methods, and fetched the code of the "Cash On Delivery" payment method. In the next CURL call, we've passed the payment_method
code as an argument which is required to set the payment method.
As a result, you should see "Success: Payment method has been set!"
How to Add Customer Data
Now, let's go ahead and set up the customer data. Create a file add_customer_data.php
with the following contents.
<?php require "common.php"; // set up params $url = 'http://your-opencart-store-url/index.php?route=api/customer'; $fields = array( 'firstname' => 'Sajal', 'lastname' => 'Soni', 'email' => 'sajalsoni@gmail.com', 'telephone' => '1111111111' ); $json = do_curl_request($url, $fields); $data = json_decode($json); var_dump($data);
Nothing extraordinary—we've just passed the required customer fields to the API! The message "You have successfully modified customers" should give you the confirmation of success.
So far, we've set up everything nicely for our order. The only remaining thing to complete our order is to make an API call to create an order, and that's the recipe of our next section!
How to Create an Order
Create a file add_order.php
with the following contents.
<?php require "common.php"; // set up params $url = 'http://your-opencart-store-url/index.php?route=api/order/add'; $fields = array( 'shipping_method' => 'free.free' ); $json = do_curl_request($url, $fields); $data = json_decode($json); var_dump($data);
Although it's pretty simple code to create a new order, the important thing to note here is that you need to pass the "Shipping Method" as an argument. Yes, it's a bit weird as we've already set up the shipping method in the earlier example, but that's how it works at the moment.
So, that's the complete process to create an order in OpenCart using REST APIs. There are a few other APIs as well to set up coupons, rewards and vouchers, but for brevity I'll leave them for you to explore!
Conclusion
In this series, we've discussed the REST APIs in OpenCart. We took a ride through the PHP cURL examples to see the usage of the APIs. I hope that it helps you to integrate third-party systems with OpenCart. Don't forget to checkout our offerings in the marketplace, and don't forget share your thoughts on this exciting feature!
by Sajal Soni via Envato Tuts+ Code
No comments:
Post a Comment