0

Can anyone please tell me how to put customer's first and last name into the header in OpenCart 2.0?

I am already using this code for OpenCart 1.5.6:

<?php echo $this->customer->getFirstName(); ?>
<?php echo $this->customer->getLastName(); ?>

But this code is not working for OC 2.0

I am getting this error : Undefined property: Loader::$customer in header.tpl

Please help me anyone.

shadyyx
  • 15,825
  • 6
  • 60
  • 95
sarath
  • 9
  • 2
  • 4
  • "But ThisCode Is Not Working" - what error message do you get? – Tilman Hausherr Dec 11 '14 at 18:58
  • I Am Getting This Error : Undefined property: Loader::$customer in header.tpl – sarath Dec 12 '14 at 06:36
  • Then please edit your question to include this. Btw I see somebody answered. If the answer is correct, then please press the checkmark. – Tilman Hausherr Dec 12 '14 at 07:01
  • Learn how to write. Only a sentence starts with uppercase letter, not each word in the sentence. In OC2.0 there is no longer `$this` nor *registry* available in template, therefore you need to prepare all the data in your controllers. – shadyyx Dec 15 '14 at 11:28

2 Answers2

3

To fix this error you need to call them in the controller instead of in the template.

In catalog/controller/common/header.php add the following code inside the index() function:

$data['customer_firstname'] = $this->customer->getFirstName();
$data['customer_lastname'] = $this->customer->getLastName();

In catalog/view/theme/your-theme/template/common/header.tpl you can echo the first and last name:

echo $customer_firstname;
echo $customer_lastname;

Note that it is better not to edit Opencart core files. Instead you can use VQMod to implement the changes in the header controller.

vicente
  • 2,613
  • 4
  • 22
  • 27
0

Hey I had a solution to add first name last name of logged in user : 1.Go To: catalog/controller/common/header.php

  1. Then find public function index () {....

  2. Then add the following code:

if ($this->customer->isLogged()) { $data['welcome_message'] = sprintf("Welcome %s %s, Enjoy your stay!", $this->customer->getFirstName(), $this->customer->getLastName()); }

  1. Now go to : catalog/view/theme/YOURTHEME/template/common/header.tpl

  2. then put this where you want :

Sona
  • 103
  • 7