Page 1 of 2

[How-to] Display cart total (items & price) in header

Posted: Sat Aug 07, 2010 2:49 pm
by Maansy
1st, i want to thank my bro kroozing for figuring this out. its totally credited to him and to Q for giving the heads up.

This add-on will show cart total (items & prices) in header. SEE EXAMPLE HERE

lets begin.
BACKUP BACKUP BACKUP

Files need to alter: 3

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

Code: Select all

<?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:

Code: Select all

class ControllerCommonHeader extends Controller {
   protected function index() {
 
add:

Code: Select all

$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:

Code: Select all

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

Code: Select all

$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 ?>:

Code: Select all

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

Thats it. Enjoy it :)

Re: [How-to] Display cart total (items & price) in header

Posted: Mon Aug 09, 2010 3:53 pm
by lxgenius
Hi Maansy

Thank you for that! That is really good! :)

I wonder if it is possible that the counter updates when I put something into the cart(press add to cart button)?!
It only updates when i reload the page or leave it.

greetz

Re: [How-to] Display cart total (items & price) in header

Posted: Tue Aug 10, 2010 6:14 am
by Maansy
well right now, if you add to cart from any page or catagory page then the site reloads then the cart it updated.
the only page the ajax is implamented is in the product page and in order to update the header cart same way as the box cart would be to let ajax send the js to the header cart which i dont think its a good idea leting to do that.
i am thinking maybe we need to add a 2nd id:

Code: Select all

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

 
you can disable the box cart and try to see when you click add to cart from the product page does the product fly to the top instead?

so many things can be done here :)

Re: [How-to] Display cart total (items & price) in header

Posted: Tue Aug 10, 2010 11:05 pm
by nnestor
Hello. I do not know whether it just makes me, but I'm getting 2x the price for them. Where can be the problem? already when I click the add to cart so there is only one price. Please help

Re: [How-to] Display cart total (items & price) in header

Posted: Wed Aug 11, 2010 12:10 am
by Maansy
please provide a link to the issue so i can see

Re: [How-to] Display cart total (items & price) in header

Posted: Wed Aug 11, 2010 12:20 am
by nnestor
http://roxy.begyben.sk
Thank you. and sorry for the English

Re: [How-to] Display cart total (items & price) in header

Posted: Wed Aug 11, 2010 12:51 am
by Maansy
its doing what it suppose to do.
cart_issue1.png

cart_issue1.png (80.86 KiB) Viewed 43435 times

what seems to be the problem?

Re: [How-to] Display cart total (items & price) in header

Posted: Wed Aug 11, 2010 12:55 am
by nnestor
But if I have 0 products in your cart so I get the following:
(0) Položiek: 0.00€0.00€

Re: [How-to] Display cart total (items & price) in header

Posted: Wed Aug 11, 2010 1:12 am
by Maansy
důvody etry najít někoho, které mohou vysvětlit lépe problém.

i am sorry. i am still trying to understand the problem that you have.
when you add product in the cart, does it show 2x the price? (it didnt when i tried it)
or
doesnt show anything? (when no pruduct it shows 0, normal)
or
another thing is happening?

Re: [How-to] Display cart total (items & price) in header

Posted: Wed Aug 11, 2010 1:16 am
by nnestor
When you reach the page, so you see two prices in the basket. (€ 0.00 € 0.00). feeder when you add something to cart it works 100%

Re: [How-to] Display cart total (items & price) in header

Posted: Wed Aug 11, 2010 1:21 am
by Maansy
ok i get the issue.
ok let see.
please try to add this to a default them and to an un-touch header.php in controller and see if the problem is gone.

Re: [How-to] Display cart total (items & price) in header

Posted: Fri Aug 13, 2010 6:34 am
by lxgenius
@maansy

Sorry, that I did't respond. I don't get email notification from this forum. I changed that.
I will try that in the weekend, .....

@nnestor

just change the code in the header.tpl like that:

Code: Select all

<?php if ($products) { ?>    
    <a href="index.php?route=checkout/cart" alt="Item(s)"><?php echo $text_cart;?>&nbsp;<?php echo $this->cart->countProducts();?>&nbsp;<?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 $text_cart;?>&nbsp;<?php echo $this->cart->countProducts();?>&nbsp;<?php echo $text_shoppingbag; ?></a>   
    <?php
    foreach ($totals as $total) { 
    if ($total['title'] != 'Sub-Total:') {
           //remove from here//
        }
       }
        echo $total['text']; <--//place here//
    } ?> 
you just move the echo $total['text']; two lines down
et voila

Re: [How-to] Display cart total (items & price) in header

Posted: Fri Aug 13, 2010 7:32 am
by Maansy
i dont know why it wasnt working with you before. its working fine with all my stores thu, but i am glad you got that fixed for you in case someone face the same issue you did :)

Re: [How-to] Display cart total (items & price) in header

Posted: Fri Aug 13, 2010 9:11 pm
by nnestor
ok, now it's very good. I would have needed if it is with Ajax. thank you

no help, huh? :-\

Re: [How-to] Display cart total (items & price) in header

Posted: Fri Aug 27, 2010 7:48 am
by nazimjamil

Re: [How-to] Display cart total (items & price) in header

Posted: Tue Aug 31, 2010 3:46 am
by grassm
How would you go about taking out the price and just showing the total number? I've tried taking out pieces in the tpl file but it results in a php error.

Re: [How-to] Display cart total (items & price) in header

Posted: Sun Sep 26, 2010 3:47 am
by tarkanlar
in 1.4.9.1 i only add this in header.tpl

Code: Select all

(<?php echo $this->cart->countProducts(); ?>)<?php echo $this->currency->format($this->cart->getTotal()); ?>
and some how it works great ))

Re: [How-to] Display cart total (items & price) in header

Posted: Sun Sep 26, 2010 6:13 pm
by TAC
Hi tarkanlar, I tried your version and it works great except that I cannot get it to go where I would like it on the page. I don't really know what I am doing with code so are you able to tell me where to place the code exactly so that it appears at the far right of the header bar?
Thanks

Re: [How-to] Display cart total (items & price) in header

Posted: Tue Oct 19, 2010 3:32 pm
by guisauer
tarkanlar, your method is so simple, and it works great if you just want to show the subtotal!

But if i want it to show:

1 item ($30.00) or
2 items ($60.00)

how do I make it change the word from item to items for more than 1 product?

thanks!!!

Re: [How-to] Display cart total (items & price) in header

Posted: Fri Oct 29, 2010 5:17 am
by harryo40
Hi,
Hope this helps people out in this thread ;)
I have updated my Mod from working with OC1.3.2 to OC1.4.9.1 which can be downloaded for FREE from here.....
http://www.opencart.com/index.php?route ... ion_id=716
It shows the cart in the header & to the right of the search box & also shows the Item count & Sub-Total that updates when the main add to cart buttons are clicked.
I have also started a Thread which can be seen here.....
http://forum.opencart.com/viewtopic.php?f=112&t=22043
Please post question, issues or any problems with the 'Cart In Header' Mod & I will reply to them from within the thread ;)
cheers
harryo