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

戏子无情 提交于 2019-12-01 11:01:16

问题


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 :)


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/16919807/how-to-get-products-in-json-format-from-opencart-using-phonegap-jquerymobile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!