0

I'm using OpenCart 2.0 and I'm trying to show a user email on a category page when a user is logged in. The code below I believe works for it to show a users email on the success.tpl. How can I display it on a category page? Thanks for your time.

IN: /catalog/controller/product/category.php

I HAVE THIS:

 $this->load->model('account/order');
    $order = $this->model_account_order->getOrder($this->session->data['order_id']);
    if($order) {
        $this->data['email'] = $order['email'];
    }

THEN IN:/catalog/view/theme/default/template/category.tpl

I HAVE THIS:

<?php if(!empty($email)) echo $email; ?>
Mark Rodriguez
  • 125
  • 2
  • 15

1 Answers1

1

This is part of the core library. You don't need to look up an order if they're logged in, you just need to use

$this->customer->getEmail();

Note that in 2.0 you can't just echo this in a template, you need to assign this in the controller to the $data array and then use the value in the template. A quick hack to just add it to the template is to use

<?php
global $customer;
echo $customer->getEmail();
?>

but really isn't recommended

Jay Gilford
  • 15,141
  • 5
  • 37
  • 56
  • Thanks I just spent hours trying to figure this out. Works great...thanks! – Mark Rodriguez Nov 26 '14 at 22:26
  • I wouldn't use `global` in templates and wouldn't call objects registered in the registry in the templates directly at all. Thus I wouldn't even show this to ppl here because usually if there is easier way of doing something but dirty, ppl here always stick to this dirty one... And then we teach ppl *how to become a poor developer* instead of *how to become a good one*. – shadyyx Nov 28 '14 at 08:22
  • @shadyyx Yeah, that's why I mentioned not using it in a template, but sometimes people don't want to make large changes for something simple. There are details on how to do this elsewhere and if people are going to do it, then we can't stop them. Also, in versions prior to 2.0 it was perfectly valid to use the `$this->customer->getEmail();` directly in templates, and some people know this but don't know how to do so for their theme in 2.0 – Jay Gilford Nov 28 '14 at 10:28