Search code examples
opencartopencart2.xopencart-module

Show customer id in header.tpl on Opencart 2


How to display customer id in header.tpl on Opencart 2?

    if ($this->customer->isLogged()) {
    $data['customer_id'] = $this->customer->getId(); // customer ID
    $data['customer_fname'] = $this->customer->getFirstName(); // customer email
    }

doesn`t work.


Solution

  • In header.php file add this two variables

    if ($this->customer->isLogged()) { // <-- This line is around 52
        $this->load->model('account/wishlist');
    
        $data['text_wishlist'] = sprintf($this->language->get('text_wishlist'), $this->model_account_wishlist->getTotalWishlist());
        $data['customer_id'] = $this->customer->getId(); // <-- add this variable
        $data['customer_fname'] = $this->customer->getFirstName(); // <-- and this variable
    } else {
        $data['text_wishlist'] = sprintf($this->language->get('text_wishlist'), (isset($this->session->data['wishlist']) ? count($this->session->data['wishlist']) : 0));
    }
    

    In header.tpl file you can use the variables like this, must be inside the if ($logged) block

    <?php if ($logged) { ?>
    <?php echo $customer_id; ?>
    <?php echo $customer_fname; ?>
    <?php } ?>
    

    Tested on OC 2.2.0.0