Advertisement
  1. Code
  2. OpenCart
Code

Explore REST APIs in OpenCart 2.0: Part Two

by
Difficulty:IntermediateLength:MediumLanguages:
This post is part of a series called Explore REST APIs in OpenCart .
Explore REST APIs in OpenCart 2.0: Part One

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.

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.

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.

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.

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.

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.

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!

Advertisement
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.