How to display cart total (items & price) in header section?

STEP 1
open: catalog\view\theme\default12\template\common\header.tpl
Add this anywhere you want to disply the info:

    <?php if ($products) { ?>    
        <a href="index.php?route=checkout/cart" alt="Item(s)">(<?php echo $this->cart->countProducts(); ?>) <?php echo $text_shoppingbag; ?></a>
        <?php
        $count=count($totals);
        $cartinfo = $totals[$count-1]['text'];
        echo $cartinfo;
        ?>   
        <?php } else { ?>  
         <a href="index.php?route=checkout/cart" alt="Item(s)">(<?php echo $this->cart->countProducts(); ?>) <?php echo $text_shoppingbag; ?></a>   
        <?php
        foreach ($totals as $total) { 
        if ($total['title'] != 'Sub-Total:') {
                echo $total['text']; 
            }
           }     
        } ?>

STEP 2
open: catalog\controller\common\header.php
after:

    class ControllerCommonHeader extends Controller {
       protected function index() {

Add:

    $this->data['products'] = array();

            foreach ($this->cart->getProducts() as $result) {
                $option_data = array();

                foreach ($result['option'] as $option) {
                      $option_data[] = array(
                        'name'  => $option['name'],
                        'value' => $option['value']
                      );
                }

                  $this->data['products'][] = array(
                    'key'          => $result['key'],
                    'name'       => $result['name'],
                    'option'     => $option_data,
                    'quantity'   => $result['quantity'],
                    'stock'      => $result['stock'],
                    'price'      => $this->currency->format($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax'))),
                    'href'       => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/product&product_id=' . $result['product_id']),
                  );
            }

       $total_data = array();
            $total = 0;
            $taxes = $this->cart->getTaxes();

            $this->load->model('checkout/extension');

            $sort_order = array(); 

            $results = $this->model_checkout_extension->getExtensions('total');

            foreach ($results as $key => $value) {
                $sort_order[$key] = $this->config->get($value['key'] . '_sort_order');
            }

            array_multisort($sort_order, SORT_ASC, $results);

        foreach ($results as $result) {
                $this->load->model('total/' . $result['key']);

                $this->{'model_total_' . $result['key']}->getTotal($total_data, $total, $taxes);
            }

            $sort_order = array(); 

            foreach ($total_data as $key => $value) {
                  $sort_order[$key] = $value['sort_order'];
            }

            array_multisort($sort_order, SORT_ASC, $total_data);

            $this->data['totals'] = $total_data;

            if (isset($this->session->data['coupon'])) {
                $this->load->model('checkout/coupon');

                $coupon = $this->model_checkout_coupon->getCoupon($this->session->data['coupon']);

                if ($coupon) {
                    $data['coupon_id'] = $coupon['coupon_id'];
                } else {
                    $data['coupon_id'] = 0;
                }
            } else {
                $data['coupon_id'] = 0;
            }

        $this->data['ajax'] = $this->config->get('cart_ajax');

            $this->id = 'cart';

and after:

    $this->data['text_advanced'] = $this->language->get('text_advanced');

add:

    $this->data['text_shoppingbag'] = $this->language->get('text_shoppingbag');

STEP 3
open: catalog\language\english\common\header.php (do this step to every language file you have)
add this before the closing ?>:

    $_['text_shoppingbag']   = 'Item(s):';

Thats it. Enjoy it 🙂

One Reply to “How to display cart total (items & price) in header section?”

  1. Got an error

    Fatal error: Call to a member function rewrite() on a non-object in /var/www/html/xxxx/catalog/controller/common/header.php on line 24

Leave a Reply

Your email address will not be published. Required fields are marked *


*