Page 1 of 1

how can load $text_items varialble in footer.tpl?

Posted: Fri Nov 09, 2012 5:39 pm
by Uberos
Hi, how can I load $text_items variable in footer.tpl?

i want to do something like this:

<?php if (substr($text_items,0)==0) {
echo "<img src=\"catalog/view/theme/default/image/emptycart.png\">";
}else{
echo "<img src=\"catalog/view/theme/default/image/cart.png\">";
}
?>

But $text_items is an undefined variable.

Thanks.

Re: how can load $text_items varialble in footer.tpl?

Posted: Mon Nov 12, 2012 4:57 pm
by Uberos
Any ideas?

Re: how can load $text_items varialble in footer.tpl?

Posted: Fri Nov 16, 2012 6:44 pm
by subdivide
Open your footer language file:

/catalog/language/english/common/footer.php

Add your variable like so:

Code: Select all

$_['your_text_variable'] = 'Text to appear in your footer';
Then open your footer controller:

/catalog/controller/common/footer.php

Add your variable to the data array of the controller:

Code: Select all

$this->data['text_variable'] = $this->language->get('your_text_variable');
Now your variable is available as $text_variable in your footer view.

Code: Select all

<?php echo $text_variable; ?>
You can also run tests against the raw text variable in the controller prior to setting the data variable.

Code: Select all

if (substr($this->language->get('your_text_variable'), 0) == 0):
	$this->data['your_image'] = '<img src="catalog/view/theme/default/image/emptycart.png">';
else:
	$this->data['your_image'] = '<img src="catalog/view/theme/default/image/cart.png">';
endif;
Then in the view just echo the image:

Code: Select all

<?php echo $your_image; ?>
Hope this helps.

-V

Re: how can load $text_items varialble in footer.tpl?

Posted: Tue Nov 20, 2012 12:35 am
by Uberos
Thanks for your answer but i think that i haven't explained right.

$text_items variable is written (echoed) in catalog/view/theme/["mytemplate"]/template/module/cart.tpl

Code: Select all

<span id="cart-total"><?php echo $text_items; ?></span></a></div>
$text_items says the number of products you have inside the cart.

I need $text_items in footer.tpl but i don't know how load it.

Re: how can load $text_items varialble in footer.tpl?

Posted: Tue Nov 20, 2012 2:13 am
by subdivide
Ah, I see.

You can get this information directly from the cart class which is available globally.

Open: /catalog/language/english/common/footer.php and add the following:

Code: Select all

$_['text_items']    = '%s item(s) - %s';
(copied from modules/cart.php)

Open: /catalog/controller/common/footer.php and add the following:

Code: Select all

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

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

// Display prices
if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
	$sort_order = array(); 
	
	$results = $this->model_setting_extension->getExtensions('total');
	
	foreach ($results as $key => $value) {
		$sort_order[$key] = $this->config->get($value['code'] . '_sort_order');
	}
	
	array_multisort($sort_order, SORT_ASC, $results);
	
	foreach ($results as $result) {
		if ($this->config->get($result['code'] . '_status')) {
			$this->load->model('total/' . $result['code']);

			$this->{'model_total_' . $result['code']}->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['text_items'] = sprintf($this->language->get('text_items'), $this->cart->countProducts() + (isset($this->session->data['vouchers']) ? count($this->session->data['vouchers']) : 0), $this->currency->format($total));
Open: catalog/view/theme/*/template/footer.tpl and add:

Code: Select all

<?php echo $text_items; ?>
Keep in mind this won't update with javascript when you add a new item, to do that you'll need to give the container an id and add that to the update section of the addToCart() method in your javascript controller.

Hope that helps.

-V

Re: how can load $text_items varialble in footer.tpl?

Posted: Wed Nov 21, 2012 8:28 pm
by Uberos
Just missing a line to complete your instructions in "/catalog/controller/common/footer.php"
copied from modules/cart.php:

Code: Select all

$this->language->load('module/cart');
it works! Now I undestand a bit more the opencart code, thankyou very much! :-)
It would be interesting update with javascript when adding the cart but i don't know how to do it :-\

If you help me i'll be thanful

Re: how can load $text_items varialble in footer.tpl?

Posted: Sun Feb 15, 2015 6:15 am
by basevinyl
Thanks for posting this I was looking for help on this for hours and you have ended my search with a step by step guide to the solution. Thanks! :)

Re: how can load $text_items varialble in footer.tpl?

Posted: Mon Feb 16, 2015 1:43 pm
by subdivide
Glad it helped :)

Re: how can load $text_items varialble in footer.tpl?

Posted: Fri Feb 20, 2015 4:08 am
by basevinyl
Hi Subdivide,

Thanks again!

I tried to duplicate this procedure to add some text to the product page and it didn't work. Could you help?

This is what I did:

Step 1: opened /catalog/language/english/product/product.php and added this: $_['your_text_variable'] = 'Text to appear in your product page';

Step 2: opened catalog/controller/product/product.php and added this: $this->data['text_variable'] = $this->language->get('your_text_variable');

Step 3: added <?php echo $text_variable; ?> to the catalog/view/template/product/product.tpl file where I wanted the text to appear.

I cleared my cache and loaded a page but no text appeared. Am I missing something or did I add the code to the wrong pages?

I was able to get it to work in the footer but not the product page.

Thanks if you could help!

Davidicus

Re: how can load $text_items varialble in footer.tpl?

Posted: Fri Feb 20, 2015 4:17 am
by subdivide
The only thing I can think of is that perhaps your language file hasn't been loaded at the point where you placed your declaration.

Make sure you place your declaration:

Code: Select all

$this->data['text_variable'] = $this->language->get('your_text_variable');
After:

Code: Select all

$this->language->load('product/product');
Other than that, I can't explain why it wouldn't render.