AbanteCart Development > Development Help Needed

How to create new response

(1/4) > >>

yonghan:
Hi admins,i'm trying to create new response which return product as json.Here are my codes :

extensions\retail\storefront\controller\responses\extension\retail.php :


--- Code: ---<?php

if (! defined ( 'DIR_CORE' )) {
    header ( 'Location: static_pages/' );
}

class ControllerResponsesExtensionRetail extends AController {
    public $data = array();

    public function main() {}


    public function getProducts()
    {
        $this->extensions->hk_InitData($this, __FUNCTION__);
        $id=$this->request->get['category_id'];
        $dispatch = $this->dispatch('responses/extension/retail/get_products',array('id'=>$id));
        $this->data['retail']=$dispatch->dispatchGetOutput();
        $this->extensions->hk_UpdateData($this, __FUNCTION__);

        $this->load->library('json');
        $this->response->addJSONHeader();
        $this->response->setOutput(AJson::encode($this->data));
    }

    public function get_products($id)
    {
        $this->extensions->hk_InitData($this,__FUNCTION__);
        $this->loadModel('catalog/model');

        $category_info = $this->model_catalog_category->getCategory($id);
        if ($category_info) {
            $this->loadModel('catalog/product');
        }

        $category_total = $this->model_catalog_category->getTotalCategoriesByCategoryId($id);
        $product_total = '';
        if ($category_total || $product_total) {
            $categories = array();
            $results = $this->model_catalog_category->getCategories($id);
            $resource = new AResource('image');
            $products = array();
            $counts=array();
            foreach ($results as $result) {
                $ids=$this->model_catalog_product->getTotalProductsByCategoryId($result['id']);

                //$ids = (int)$result['id'];
                $subcategories=$this->model_catalog_product->getProductsByCategoryId($result['category_id']);
                $counts[] = $ids;

                foreach ($subcategories as $subs) {
                    $product_ids[] = (int)$subs['product_id'];
                }
                $products_info = $this->model_catalog_product->getProductsAllInfo($product_ids);
                foreach ($subcategories as $subs) {

                    $thumbnail = $resource->getMainThumb('products',
                        $subs['product_id'],
                        (int)$this->config->get('config_image_product_width'),
                        (int)$this->config->get('config_image_product_height'), true);

                    $rating = $products_info[$subs['product_id']]['rating'];
                    $special = FALSE;
                    $discount = $products_info[$subs['product_id']]['discount'];

                    if ($discount) {
                        if ($this->currency->getCode()=="IDR")
                        {
                            $price = $this->currency->format($this->tax->calculate($discount, $subs['tax_class_id'], $this->config->get('config_tax')));
                            $val=(int)preg_replace('/([^0-9\.,])/i','',$price);
                        }
                        else
                        {
                            $price = $this->currency->format($this->tax->calculate($discount, $subs['tax_class_id'], $this->config->get('config_tax')));
                            $val=(int)preg_replace('/([^0-9\.,])/i','',$price);
                        }
                    } else {
                        $price = $this->currency->format($this->tax->calculate($subs['price'], $subs['tax_class_id'], $this->config->get('config_tax')));
                        if ($this->currency->getCode()=="IDR")
                        {
                            $val=(int)$this->tax->calculate($subs['price'], $subs['tax_class_id'], $this->config->get('config_tax'));
                        }
                        else
                        {
                            $val=(int)preg_replace('/([^0-9\.,])/i', '',$this->currency->format($this->tax->calculate($subs['price'], $subs['tax_class_id'], $this->config->get('config_tax'))));
                        }
                        $special = $products_info[$subs['product_id']]['special'];
                        if ($special) {
                            if ($this->currency->getCode()=="IDR")
                            {
                                $special = $this->currency->format($this->tax->calculate($special, $subs['tax_class_id'], $this->config->get('config_tax')));
                                $val=(int)preg_replace('/([^0-9\.])/i','',$special);
                            }
                            else
                            {
                                $special = $this->currency->format($this->tax->calculate($special, $subs['tax_class_id'], $this->config->get('config_tax')));
                                $val=(int)preg_replace('/([^0-9\.,])/i','',$special);

                            }
                        }
                    }

                    $options = $products_info[$subs['product_id']]['options'];

                    if ($options) {
                        $add = $this->html->getSEOURL('product/product', '&product_id=' . $subs['product_id'], '&encode');
                    } else {
                        if ($this->config->get('config_cart_ajax')) {
                            $add = '#';
                        } else {
                            $add = $this->html->getSecureURL('checkout/cart', '&product_id=' . $subs['product_id'], '&encode');
                        }
                    }
                    $products[] = array(
                        'product_id' => $subs['product_id'],
                        'name' => $subs['name'],
                        'model' => $subs['model'],
                        'rating' => $rating,
                        'stars' => sprintf($this->language->get('text_stars'), $rating),
                        'thumb' => $thumbnail,
                        'price' => $price,
                        'call_to_order' => $subs['call_to_order'],
                        'options' => $options,
                        'special' => $special,
                        'href' => $this->html->getSEOURL('product/product', '&path=' . $this->request->get['path'] . '&product_id=' . $subs['product_id'], '&encode'),
                        'add' => $add,
                        'description' => html_entity_decode($subs['description'], ENT_QUOTES, 'UTF-8'),
                        'category_name'=>$result['name'],
                        'val'=>$val,
                        'date'=>$subs['date_available']
                    );
                }
                $this->data['products'] = $products;
            }



            $this->view->assign('categories', $categories);
            $this->loadModel('catalog/review');
            $this->view->assign('button_add_to_cart', $this->language->get('button_add_to_cart'));

            if ($this->config->get('config_customer_price')) {
                $display_price = TRUE;
            } elseif ($this->customer->isLogged()) {
                $display_price = TRUE;
            } else {
                $display_price = FALSE;
            }
            $this->view->assign('display_price', $display_price);


            $this->view->assign( 'url', $this->html->getSEOURL('product/category','&path=' . $this->request->get['path']));

            $this->view->batchAssign($this->data);

    }
        if(!$this->view->isTemplateExists('responses/extension/retail.tpl')){
            return '';
        }


        //update controller data
        $this->extensions->hk_UpdateData($this, __FUNCTION__);
        $this->processTemplate('responses/extension/retail.tpl');

    }
}

--- End code ---

extensions\retail\storefront\view\retail\template\responses\extension\retail.tpl :


--- Code: ---<div class="products-layout grid m-t-b" data-product=".product" data-thumbnail=".item-image-container .thumb" data-title="itema" data-url=".item-name > a" data-price=".item-price">
    <?php

    $limit=$this->getHookVar('limit');
    foreach ($products as $product) {
    $item = array();
    $item['image'] = $product['thumb']['thumb_html'];
    $item['title'] = $product['name'];
    $item['description'] = $product['model'];
    $item['rating'] = ($product['rating']) ? "<img src='" . $this->templateResource('/image/stars_' . $product['rating'] . '.png') . "' alt='" . $product['stars'] . "' />" : '';
    $item['catname']=$product['category_name'];
    $item['product_id']=$product['product_id'];
    $item['val']=$product['val'];
    $item['info_url'] = $product['href'];
    $item['buy_url'] = $product['add'];
    $item['date']=$product['date'];

    if (!$display_price) {
    $item['price'] = '';
    }

    $review = $button_write;
    if ($item['rating']) {
    $review = $item['rating'];
    }

    ?>
    <div class="product" data-time="<?php echo $product['date']; ?>" data-title="<?php echo $item['title']; ?>" data-price="<?php echo $item['val']; ?>" data-product-id="<?php echo $item['product_id']; ?>" data-category="<?php echo mb_strtolower($heading_title);?>-<?php echo mb_strtolower($item['catname']); ?>">

        <div class="item clearfix">
            <div class="item-image-container">
                <figure>
                    <a href="<?php echo $item['info_url'] ?>">
                        <?php echo $item['image'] ?>

                    </a>
                </figure>
                <?php if ($display_price) { ?>

                <?php if ($product['special']) { ?>
                <div class="item-price-container">
                    <div class="old-price"><?php echo $product['price'] ?></div>
                    <div class="item-price"><?php echo $product['special'] ?></div>
                </div>
                <?php } else { ?>
                <div class="item-price-container">
                    <div class="item-price price"><?php echo $product['price'] ?></div>
                </div>
                <?php } ?>
                <?php } ?>


                <?php if ($product['special']) { ?>
                <span class="new-rect"><?php echo $text_sale_label; ?></span>
                <?php } ?>
                <?php if ($product['new_product']) { ?>
                <span class="new-rect"><?php echo $text_new_label; ?></span>
                <?php } ?>

            </div><!-- End .item-image -->
            <div class="item-meta-container entry-main">

                <h3 class="item-name name"><a id="itema" href="<?php echo $item['info_url'] ?>"><?php echo $item['title'] ?></a></h3>
                <div class="entry-description visible-list">
                    <?php echo $product['description'] ?>
                </div>
                <div class="item-action">
                    <a data-label="<?php echo $product['name'] ?>" data-id="<?php echo $product['product_id'] ?>"
                       href="<?php echo $item['buy_url'] ?>" class="item-add-btn">
                        <span class="icon-cart-text"><?php echo $button_add_to_cart ?></span>
                    </a>

                </div><!-- End .item-action -->
            </div><!-- End .item-meta-container -->

        </div>
    </div><!-- End .col-md-4 -->



    <?php
    }
?>

</div>
<script type="text/javascript">



    //console.log($('.range-slider').data("min"));
    //var price= $('.item-price-container > .item-price').text();
    //alert(price);


</script>

--- End code ---

js file :


--- Code: ---$('#cl').find('input[type="checkbox"]').on('change', function () {
        $('#cl').find('input[type="checkbox"]:checked').each(function () {

            $.ajax({
                url:'<?php echo $get_product_url; ?>' ,
                type:'GET',
                dataType:'json',
                data: {category_id:  '36' },
                success:function (data) {
                    console.log(data);
                }
            });
        });
    });

--- End code ---

and on my extension core :


--- Code: ---<?php


if (!defined('DIR_CORE')) {
    header('Location: static_pages/');
}

class ExtensionRetail extends Extension
{
public function onControllerPagesProductCategory_UpdateData()
    {
     $that->view->assign('get_product_url', $that->html->getURL('r/extension/retail/getProduct'));
    }
}

--- End code ---

When i try to run the code,i got this on my error log :


--- Code: ---2014-07-25 7:10:14 - App Warning:  AbanteCart core v.1.1.9 ADispatch: responses/error/not_found construct FAILED. Missing or incorrect controller route path. Possibly layout block is enabled for disabled or missing extension!  in E:\php\xampp\htdocs\AbanteCart\core\engine\dispatcher.php on line 76

--- End code ---

It returns 200 OK response on firebug console.

Please point me where are the mistakes.Thanks a lot.

abolabo:
enable debug in settings first. Then you can see what's going on in ADispatcher. Of not? you can add backtrace() into ADispatcher to define who call it wrong.

Also do not forget to add in main.php of your extension all controllers of extension.

yonghan:
Hi,i have defined the controller and the template file in main.php.I will try to use the debug first.Thanks

yonghan:
Hi abolabo,i would like to which debug level should i choose?Thanks

abolabo:

--- Quote from: yonghan on July 25, 2014, 01:25:30 AM ---When i try to run the code,i got this on my error log :


--- Code: ---2014-07-25 7:10:14 - App Warning:  AbanteCart core v.1.1.9 ADispatch: responses/error/not_found construct FAILED. Missing or incorrect controller route path. Possibly layout block is enabled for disabled or missing extension!  in E:\php\xampp\htdocs\AbanteCart\core\engine\dispatcher.php on line 76

--- End code ---

It returns 200 OK response on firebug console.

Please point me where are the mistakes.Thanks a lot.

--- End quote ---

responses/error/not_found  - abantecart have no this "response"- controller... only "page"

Navigation

[0] Message Index

[#] Next page

Go to full version
Powered by SMFPacks Social Login Mod