2

Is there anyway, to fetch product catalog in JSON format from my OpenCart store, from a phonegap mobile application using Ajax, JavaScript/jQuery. Does OpenCart allow for such a thing?

Any ideas or code are welcome :)

2 Answers 2

2

OcJoy is going right way but the solution provided will contain all the page data which I guess is not Your desired output.

Instead of his solution, You should do something like this (assuming You need only the products JSON array) before $this->response->setOutput($this->render());:

if(isset($this->request->get['json'])) {
    echo json_encode($this->data['products']);
    die;
} else

Then after hitting http://www.example.com/index.php?route=product/category&path=20&json only the JSON of products array of category with ID 20 will be output.


EDIT: to call this from within a template as an AJAX request, You could do:

$(document).ready(function(){
    $.ajax({
        url: 'index.php?route=product/category&path=<CATEGORY_ID>&json',
        type: 'get',
        dataType: 'json',
        beforeSend: function() {
        },
        complete: function() {
        },
        success: function(data) {
            if (data.length > 0) {
                // do something here with the products in data array
            }
        }
    });
});

Make sure to replace <CATEGORY_ID> with the right value in the URL above and to add the desired functionality within the success callback function.

7
  • Hey thank you so much for the example, if it is possible would you mind giving me an example, how to consume this with .ajax. I have done this before but only consuming jsonp, and I know jQuery adds a ?callback after my url. How can I get the data from this ?
    – Lali Pali
    Jun 5, 2013 at 9:43
  • @DaimanLoks What do You mean by calling this from AJAX? Like from which template (page)? OpenCart is full of AJAX calls, You could get an example from e.g. catalog/view/theme/default/template/account/register.tpl or .../template/product/product.tpl - there should be a couple of jQuery.load() or jQuery.post() calls... If You'd encounter problems finding an appropriate example, let me know.
    – shadyyx
    Jun 5, 2013 at 10:16
  • Sorry I mean, calling it using the jQuery .ajax method. As you can see I am not very savy when it comes to this technologies.
    – Lali Pali
    Jun 5, 2013 at 11:02
  • I am getting this error, when trying to get the data. Access-Control-Allow-Origin I am using actually a module called opencart-webap that allows you to fetch data as a RESTapi.
    – Lali Pali
    Jun 5, 2013 at 11:16
  • @DaimanLoks Aren't You trying to load the products from http://domain1.com/ into another installation at http://otherdomain.com/??? I guess this goes behind the scope of this question and should be asked as a new problem regarding the module and full problem description... Huh?
    – shadyyx
    Jun 5, 2013 at 11:31
1

In catalog/controller/product/catalog.php before

$this->response->setOutput($this->render());

paste this code

if (isset( $this->request->get['json'])) $this->response->setOutput(json_encode($this->data));  else    

and go link http://opencart.examle/index.php?route=product/category&path=20&json

3
  • I specifically checked the code again on different versions 1.5.x . If you replace $this->response->setOutput($this->render()); to this if(isset( $this->request->get['json'])) $this->response->setOutput(json_encode($this->data)); else $this->response->setOutput($this->render());
    – OcJoy
    Jun 5, 2013 at 10:35
  • You can get the full json response, including breadcrumbs,pagination,language,products and category info by my solution
    – OcJoy
    Jun 5, 2013 at 10:42
  • OK, I misunderstood the response, I thought it would be rendered as an HTML but with json encoded data. Anyway, OP requested a solution for just the products array not whole page with useless breadcrumbs, categories, category description, sorting and paging options, etc...
    – shadyyx
    Jun 5, 2013 at 11:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.