Please help us to make AbanteCart Ideal Open Source Ecommerce Solution for everyone.

Support AbanteCart eCommerce

Author Topic: Hook Storefront Header Controller  (Read 7530 times)

yonghan

  • Guest
Hook Storefront Header Controller
« 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:

Code: [Select]
$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 :
Code: [Select]
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.
« Last Edit: October 25, 2014, 08:19:28 AM by handoyo »

Offline eCommerce Core

  • Administrator
  • Hero Member
  • *****
  • Posts: 1600
  • Karma: +93/-1
    • View Profile
Re: Hook Storefront Header Controller
« Reply #1 on: October 25, 2014, 12:48:31 PM »
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

  • Guest
Re: Hook Storefront Header Controller
« Reply #2 on: October 25, 2014, 01:00:51 PM »
I define

Code: [Select]
$that=$this-baseObject;
By the way i was trying to hook onControllerCommonHeader_UpdateData()


yonghan

  • Guest
Re: Hook Storefront Header Controller
« Reply #3 on: November 08, 2014, 09:42:12 PM »
Hi admins,I have tried to use
Code: [Select]
$this->tax->calculate(....

It doesn't work.Here is my full code,in case i was missing something within the code.

Code: [Select]
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();
    }
« Last Edit: November 08, 2014, 09:46:13 PM by handoyo »

Offline abolabo

  • core-developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 1782
  • Karma: +247/-11
  • web for all, all for web!
    • View Profile
    • AbanteCart
Re: Hook Storefront Header Controller
« Reply #4 on: November 10, 2014, 10:27:35 AM »
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.
Code: [Select]
$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

  • Guest
Re: Hook Storefront Header Controller
« Reply #5 on: November 10, 2014, 10:32:42 AM »
Thanks abolabo,I'll give it a try and let you know the result.

yonghan

  • Guest
Re: Hook Storefront Header Controller
« Reply #6 on: November 10, 2014, 10:41:38 AM »
Still not working.

Offline abolabo

  • core-developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 1782
  • Karma: +247/-11
  • web for all, all for web!
    • View Profile
    • AbanteCart
Re: Hook Storefront Header Controller
« Reply #7 on: November 10, 2014, 11:44:44 AM »
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

  • Guest
Re: Hook Storefront Header Controller
« Reply #8 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?

Offline abolabo

  • core-developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 1782
  • Karma: +247/-11
  • web for all, all for web!
    • View Profile
    • AbanteCart
Re: Hook Storefront Header Controller
« Reply #9 on: November 10, 2014, 12:31:04 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

  • Guest
Re: Hook Storefront Header Controller
« Reply #10 on: November 10, 2014, 12:45:51 PM »
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. :)

 

Powered by SMFPacks Social Login Mod