News:

AbanteCart v1.4.2 is released.

Main Menu
support

Hook Storefront Header Controller

Started by yonghan, October 25, 2014, 08:11:00 AM

Previous topic - Next topic

yonghan

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.

eCommerce Core

What is $that in your case? Where is it set?

Try this instead.
$this->tax->calculate(...

"If you're in the luckiest one per cent of humanity, you owe it to the rest of humanity to think about the other 99 per cent."
― Warren Buffett

yonghan

I define

$that=$this-baseObject;

By the way i was trying to hook onControllerCommonHeader_UpdateData()


yonghan

#3
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();
    }


abolabo

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);
"No one is useless in this world who lightens the burdens of another."
― Charles Dickens

yonghan

Thanks abolabo,I'll give it a try and let you know the result.

yonghan


abolabo

may be you tried to hook wrong controller? try to hook ControllerBlocksCart instead ControllerCommonHeader
"No one is useless in this world who lightens the burdens of another."
― Charles Dickens

yonghan

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?

abolabo

Quote from: handoyo on November 10, 2014, 12:14:22 PM
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.
"No one is useless in this world who lightens the burdens of another."
― Charles Dickens

yonghan

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

Forum Rules Code of conduct
AbanteCart.com 2010 -