AbanteCart Community
AbanteCart Development => Development Help Needed => Topic started by: yonghan on July 25, 2014, 01:25:30 AM
-
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 :
<?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');
}
}
extensions\retail\storefront\view\retail\template\responses\extension\retail.tpl :
<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>
js file :
$('#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);
}
});
});
});
and on my extension core :
<?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'));
}
}
When i try to run the code,i got this on my error log :
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
It returns 200 OK response on firebug console.
Please point me where are the mistakes.Thanks a lot.
-
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.
-
Hi,i have defined the controller and the template file in main.php.I will try to use the debug first.Thanks
-
Hi abolabo,i would like to which debug level should i choose?Thanks
-
When i try to run the code,i got this on my error log :
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
It returns 200 OK response on firebug console.
Please point me where are the mistakes.Thanks a lot.
responses/error/not_found - abantecart have no this "response"- controller... only "page"
-
Hi abolabo,what about the response controller for the product which was located on storefront/responses/product/product.php?Just now i also try to create extension by using the developer tools and try to output the json.I got the same error log.Please point me.thanks a lot
-
try to find "responses/error/not_found" or "r/error/not_found" in your code. it's a wrong RT that you call.
Your error not related to your attempt to hook responses/product/product controller.
-
also check RT for your response-controller.. probably we have a minor bug in ARouter.. We will check..
anyway you tried to call controller that you do not have on file system or route is wrong..or name of class is wrong
Look on method _detect_controller in engine/route.php
-
Hi,i suppose if my response controller was located on responses/extension/retail.php and the function called getProduct,the url should be rt=r/extension/retail/getProduct isn't it?Thanks
-
Hi,i suppose if my response controller was located on responses/extension/retail.php and the function called getProduct,the url should be rt=r/extension/retail/getProduct isn't it?Thanks
yes.
In case when you do not have page-controller by path "pages/extension/retail" you can use rt without type-prefix. Just "extension/retail/getProduct".
We added "r" as sign to recognize what type controlller need to call.
-
Just now i try to directly use rt=extension/retail/getProducts,it gives me 404 error. My function name is getProducts.It's weird. I attach the folder route.Thanks
-
please send me your response controller and main.php. i need to look
-
This is the response controller 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');
}
}
main.php
<?php
if (! defined ( 'DIR_CORE' )) {
header ( 'Location: static_pages/' );
}
if(!class_exists('ExtensionRetail')){
include_once('core/Retail_hooks.php');
}
$controllers = array(
'storefront' => array('responses/extension/retail.php'),
'admin' => array());
$models = array(
'storefront' => array('catalog/category',
'catalog/product'),
'admin' => array());
$templates = array(
'storefront' => array(
'blocks/account.tpl',
'blocks/bestseller.tpl',
'blocks/bestseller_home.tpl',
'blocks/breadcrumbs.tpl',
'blocks/cart.tpl',
'blocks/cart_top.tpl',
'blocks/category.tpl',
'blocks/category_bottom.tpl',
'blocks/category_top.tpl',
'blocks/content.tpl',
'blocks/content_footer.tpl',
'blocks/content_header.tpl',
'blocks/coupon_form.tpl',
'blocks/credit_cards.tpl',
'blocks/currency.tpl',
'blocks/donate.tpl',
'blocks/featured.tpl',
'blocks/featured_home.tpl',
'blocks/google_talk.tpl',
'blocks/html_block.tpl',
'blocks/html_block_content.tpl',
'blocks/html_block_footer.tpl',
'blocks/html_block_header.tpl',
'blocks/language.tpl',
'blocks/latest.tpl',
'blocks/latest_home.tpl',
'blocks/listing_block/popular_brands_content_bottom.tpl',
'blocks/listing_block_column_left.tpl',
'blocks/listing_block_column_right.tpl',
'blocks/listing_block_content_bottom.tpl',
'blocks/listing_block_content_top.tpl',
'blocks/listing_block_footer.tpl',
'blocks/listing_block_footer_top.tpl',
'blocks/listing_block_header.tpl',
'blocks/listing_block_header_bottom.tpl',
'blocks/manufacturer.tpl',
'blocks/menu.tpl',
'blocks/menu_bottom.tpl',
'blocks/menu_top.tpl',
'blocks/newsletter_signup_column_left.tpl',
'blocks/newsletter_signup_column_right.tpl',
'blocks/newsletter_signup_content_bottom.tpl',
'blocks/newsletter_signup_content_top.tpl',
'blocks/newsletter_signup_footer.tpl',
'blocks/newsletter_signup_footer_top.tpl',
'blocks/newsletter_signup_header.tpl',
'blocks/newsletter_signup_header_bottom.tpl',
'blocks/order_summary.tpl',
'blocks/product_list.tpl',
'blocks/search.tpl',
'blocks/search_bottom.tpl',
'blocks/search_top.tpl',
'blocks/special.tpl',
'blocks/special_home.tpl',
'common/column_left.tpl',
'common/column_right.tpl',
'common/content_bottom.tpl',
'common/content_top.tpl',
'common/footer.tpl',
'common/footer_top.tpl',
'common/head.tpl',
'common/header.tpl',
'common/header_bottom.tpl',
'common/menu.tpl',
'common/page.tpl',
'common/resource_image.tpl',
'common/success.tpl',
'common/template_debug.tpl',
'common/unsubscribe.tpl',
'form/button.tpl',
'form/captcha.tpl',
'form/checkbox.tpl',
'form/checkboxgroup.tpl',
'form/countries_zones.tpl',
'form/date.tpl',
'form/file.tpl',
'form/form.tpl',
'form/form_close.tpl',
'form/form_field.tpl',
'form/form_group.tpl',
'form/form_no_group.tpl',
'form/form_open.tpl',
'form/hidden.tpl',
'form/input.tpl',
'form/pagination.tpl',
'form/radio.tpl',
'form/rating.tpl',
'form/selectbox.tpl',
'form/submit.tpl',
'form/textarea.tpl',
'mail/order_confirm.tpl',
'pages/account/account.tpl',
'pages/account/address.tpl',
'pages/account/addresses.tpl',
'pages/account/create.tpl',
'pages/account/download.tpl',
'pages/account/edit.tpl',
'pages/account/forgotten.tpl',
'pages/account/history.tpl',
'pages/account/invoice.tpl',
'pages/account/login.tpl',
'pages/account/newsletter.tpl',
'pages/account/password.tpl',
'pages/account/subscriber.tpl',
'pages/account/transactions.tpl',
'pages/checkout/address.tpl',
'pages/checkout/cart.tpl',
'pages/checkout/confirm.buttons.tpl',
'pages/checkout/confirm.tpl',
'pages/checkout/guest_step_1.tpl',
'pages/checkout/guest_step_2.tpl',
'pages/checkout/payment.buttons.tpl',
'pages/checkout/payment.tpl',
'pages/checkout/shipping.buttons.tpl',
'pages/checkout/shipping.tpl',
'pages/content/contact.tpl',
'pages/content/content.tpl',
'pages/content/payment_confirmation.tpl',
'pages/content/sitemap.tpl',
'pages/error/not_found.tpl',
'pages/index/home.tpl',
'pages/index/maintenance.tpl',
'pages/product/category.tpl',
'pages/product/manufacturer.tpl',
'pages/product/product.tpl',
'pages/product/product_listing.tpl',
'pages/product/search.tpl',
'pages/product/special.tpl',
'responses/checkout/cart_details.tpl',
'responses/checkout/no_payment.tpl',
'responses/content/content.tpl',
'responses/extension/retail.tpl',
'responses/product/review.tpl'),
'admin' => array());
$languages = array(
'storefront' => array(),
'admin' => array());
-
$controllers = array(
'storefront' => array('responses/extension/retail'), // <--- without .php !
'admin' => array());
-
Thanks a lot,i missed that part. :D
Another question,is it possible to use existing category and product model and copy it into the model folder?
-
Thanks a lot,i missed that part. :D
Another question,is it possible to use existing category and product model and copy it into the model folder?
inside hook? yes
just load model that needed via $that->loadModel('catalog/product'); and call $that->model_catalog_product->method();
-
How about from the response controller?
-
inside extension controllers the same way as in core-controllers
-
Thanks a lot for the help abolabo.I got it working as expected.Once again thanks a lot.