How to get products in JSON format from OpenCart using phonegap/jQueryMobile

前端 未结 2 1977
花落未央
花落未央 2021-01-16 07:17

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 su

2条回答
  •  攒了一身酷
    2021-01-16 08:14

    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=&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 with the right value in the URL above and to add the desired functionality within the success callback function.

提交回复
热议问题