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

前端 未结 2 1978
花落未央
花落未央 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:06

    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

    0 讨论(0)
  • 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=<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.

    0 讨论(0)
提交回复
热议问题