Advertisement
  1. Code
  2. OpenCart
Code

How to Use Ajax With OpenCart

by
Difficulty:IntermediateLength:MediumLanguages:

The inclusion of jQuery libraries in the core of OpenCart makes Ajax implementation a breeze and more than a pleasant experience. In fact, you'll find several snippets spread across the whole framework that show the heavy use of jQuery, if you try to dig into the view files specifically.

Throughout the course of this article, we'll build a custom page to demonstrate the use of Ajax in OpenCart. It'll be a simple interface that allows you to select a product from the drop-down box and displays a nice product summary block of the selected product. The interesting part of the use-case is the way the product summary block is built—it'll be prepared using Ajax on-the-fly. Of course, it's not something that makes it an out of the world example, but I guess it'll serve the basic purpose of showing how things work in OpenCart.

I assume that you're using the latest version of OpenCart, which is 2.1.x.x! Also, the primary discussion of this article concentrates on Ajax with OpenCart, so I'll skid through the basics of custom module development in OpenCart. However, if you're not familiar with it, a quick explanation of the code snippets in between makes sure that you can follow till the end!

A Quick Glance at File Organization

Let’s quickly go through the file setup required for our custom page.

  • catalog/controller/ajax/index.php: It's a controller file that provides the application logic of the usual controller in OpenCart.
  • catalog/language/english/ajax/index.php: It's a language file that helps set up language labels.
  • catalog/view/theme/default/template/ajax/index.tpl: It's a view template file that holds the XHTML of the custom page.
  • catalog/view/theme/default/template/ajax/product.tpl: It's a view template file that holds the XHTML of the AJAX response.

So, that’s a quick list of the files we’re going to implement today.

Create Module Files

Go ahead and create a file catalog/controller/ajax/index.php with the following contents.

To start with, the index method of the controller is used to load the language and model files and set up the common variables for the usual OpenCart template. We're loading the product model available in the core itself, so we don't have to duplicate the code to fetch the product information.

After loading the product model, we're using the getProducts method to load all the products. Finally, we conclude the index method by setting index.tpl as our main template file.

Next is the important ajaxGetProduct method, which is used to build a product summary block based on the product id passed in the Ajax call, as we'll see soon in the template file. It loads the same product model as we did in the index method, and calls the getProduct method to fetch specific product information based on the product id.

At the end, the product.tpl template is set as a template for this method. Specific to the requirements in our case, we're using the template to build our Ajax output, but you could also send the JSON response instead.

Moving ahead, let's create a language file catalog/language/english/ajax/index.php to hold the static label information.

The view template file, one of the most important files in the context of this tutorial, should be created at catalog/view/theme/default/template/ajax/index.tpl with the following contents.

The snippet of our interest is at the end of index.tpl, the JavaScript code that makes use of jQuery methods to bind change and Ajax events. When the user selects a product from the drop-down box, the change event is fired that eventually makes an Ajax call. In the Ajax call, we're sending the product_id appended as a GET query-string variable.

On the other hand, as we've already discussed in the controller setup, the ajaxGetProduct sends the XHTML of the product summary block based on the product_id query-string variable. In the success method, we append the XHTML response to the div tag that has the id attribute set to product_summary.

Finally, go ahead and make a template file catalog/view/theme/default/template/ajax/product.tpl with the following contents for an Ajax call.

Nothing fancy here—We've just included a basic product summary block XHTML.

So, that's it as far as the file setup is concerned. In our next section, we'll go through the front-end to test what we've built so far.

Front-End Testing

So we've done all the hard work, and now it’s time for some testing! Head over to the front-end of OpenCart and visit the URL http://www.yourstore.com/index.php?route=ajax/index. It should display a nice-looking interface as shown in the following screenshot.

Custom Page Drop-Down

It’s our custom page, and it’s displaying a drop-down box containing the list of all products. Now, let’s try to select a product from the select box, and it’ll make an Ajax call in the background.

As a result, you should see a nice product summary block displayed right under the drop-down box as shown in the following screenshot.

AJAX Preview

If you've followed the tutorial and implemented all the files as explained, it should work smoothly for you as well as it did for me! Of course, that was a pretty simple demonstration of how Ajax works in OpenCart, but you could stretch it to the next level as per your requirements.

Go ahead and play with it, and try to make some interactive stuff using Ajax as it's the best way to learn. So, that's it for today, and I'll be back soon with some more exciting stuff.

Conclusion

It was Ajax with OpenCart that was the central attraction of today's article. As usual, we took a custom module approach to demonstrate it and made a simple yet effective use-case. 

As always, if you're looking for additional OpenCart tools, utilities, extensions, and so on that you can leverage in your own projects or for your own education, don't forget to see what we have available in the marketplace.

I hope that it was informative and useful, and don't hesitate to leave your queries and comments!

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.