AbanteCart Community
AbanteCart Development => Development Help Needed => Topic started by: yonghan on October 25, 2014, 08:11:00 AM
-
Hi admin,i would like to ask again.I'm trying to hook the storefront header controller.Here is the code i use in my hook:
$display_totals = $that->extensions->buildTotalDisplay($cart);
$that->data['totals'] = $display_totals['total_data'];
$that->data['subtotal'] = $that->currency->format($that->tax->calculate($display_totals['total'], $result['tax_class_id'], $that->config->get('config_tax')));
$that->data['taxes'] = $display_totals['taxes'];
$that->data['ajax'] = $that->config->get('cart_ajax');
$that->view->batchAssign($that->data);
$this->processTemplate();
The problem is when i open the admin page,it gives me this error :
Fatal error: Call to a member function calculate() on a non-object in E:\xampp\htdocs\ab\extensions\ritzhi\core\ritzhi_hooks.php on line 71
I have looked at the Extension Developer Guide.I have hooked several files without problem.This is my first time hook header controller.Did i miss some parts of codes?Thanks a lot.
-
What is $that in your case? Where is it set?
Try this instead.
$this->tax->calculate(...
-
I define
$that=$this-baseObject;
By the way i was trying to hook onControllerCommonHeader_UpdateData()
-
Hi admins,I have tried to use
$this->tax->calculate(...
.
It doesn't work.Here is my full code,in case i was missing something within the code.
public function onControllerCommonHeader_InitData()
{
$that=$this->baseObject;
$that->loadLanguage('total/total');
$that->loadLanguage('blocks/cart');
$that->data['heading_title'] = $that->language->get('heading_title');
$that->data['text_subtotal'] = $that->language->get('text_subtotal');
$that->data['text_empty'] = $that->language->get('text_empty');
$that->data['text_remove'] = $that->language->get('text_remove');
$that->data['text_confirm'] = $that->language->get('text_confirm');
$that->data['text_view'] = $that->language->get('text_view');
$that->data['text_checkout'] = $that->language->get('text_checkout');
$that->data['text_items'] = $that->language->get('text_items');
$that->data['text_total'] = $that->language->get('text_total');
$that->data['view'] = $that->html->getURL('checkout/cart');
$that->data['remove'] = $that->html->getURL('r/checkout/cart');
$that->data['checkout'] = $that->html->getURL('checkout/shipping');
$products = array();
$qty = 0;
$resource = new AResource('image');
$results=$that->extensions->hkgetProducts($cart);
foreach ($results as $result) {
$option_data = array();
$thumbnail = $resource->getMainThumb('products',
$result['product_id'],
$that->config->get('config_image_product_width'),
$that->config->get('config_image_product_height'),true);
foreach ($result['option'] as $option) {
$option_data[] = array(
'name' => $option['name'],
'value' => $option['value']
);
}
$qty += $result['quantity'];
$products[] = array(
'key' => $result['key'],
'name' => $result['name'],
'option' => $option_data,
'quantity' => $result['quantity'],
'stock' => $result['stock'],
'price' => $that->currency->format($that->tax->calculate($result['price'], $result['tax_class_id'], $that->config->get('config_tax'))),
'href' => $that->html->getSEOURL('product/product','&product_id=' . $result['product_id'],true),
'thumb' => $thumbnail,
);
}
$that->data['products'] = $products;
$that->data['total_qty'] = $qty;
$display_totals = $that->extensions->hkbuildTotalDisplay($cart);
$that->data['totals'] = $display_totals['total_data'];
$that->data['subtotal'] = $that->currency->format($this->tax->calculate($display_totals['total'], $result['tax_class_id'], $that->config->get('config_tax')));
$that->data['taxes'] = $display_totals['taxes'];
$that->data['ajax'] = $that->config->get('cart_ajax');
$that->view->batchAssign($that->data);
$this->processTemplate();
}
-
try to move code into _UpdatData hook (except loadLanguage()). Languages must be loaded in InitData.
Also you can override template variables via Aview class. For ex.
$new_total = 100.00;
$this->baseObject->view->assign('total', $new_total);
-
Thanks abolabo,I'll give it a try and let you know the result.
-
Still not working.
-
may be you tried to hook wrong controller? try to hook ControllerBlocksCart instead ControllerCommonHeader
-
I want to show the cart calculated price on header,that's why I hook the ControllerCommonHeader.But perhaps it's because the cart is empty so the hook doesn't work,isn't it?
-
I want to show the cart calculated price on header,that's why I hook the ControllerCommonHeader.But perhaps it's because the cart is empty so the hook doesn't work,isn't it?
yes, i understood.
Header consist of 8 blocks. Every block have it's own controller and tpl. Do not hook ControllerCommonHeader, try to hook ControllerBlocksCart instead.
To check is your variable changed in your hook just paste var_dump($your_variable_name); into public_html/storefront/view/default_html5/template/blocks/cart.tpl
Anyway enable template Debug mode to view block's controller you needed in settings.
-
I got it abolabo,the reason why i want to hook CommonHeader is i got a responsive theme that i want to use in abantecart,and the cart menu was divided into two section.Currently I'm using javascript as a quick solution.In case I can't get it to work as expected,then I need to stick with the current solution.Thanks a lot for the help abolabo. :)