user3062373
user3062373

Reputation: 13

Getting the customer email address to success.tpl in OpenCart

I have been able to get the order_id and the total order value to process them on the success page in OpenCart (v. 1.5.4) by applying the very helpful suggestions of Shadyyx (thank you!) in Opencart successful order ID and Total from JavaScript. However, I have been unable to get the email address of the (registered or guest) customer across, i.e.:

If I insert $this->data['email'] = $this->session->data ['email']; in success.php before $this->cart->clear();

I get an Undefined Index error in success.php when submitting an order.

If I insert $this->data['email'] = $this->cart->getEmail();

instead, I avoid the Undefined Index error, but still the email address does not load in the html form via the input tag (this does, however, work for the order_id and the total) as follows:

<?php if(!empty($email)): ?> <input name="email" type="hidden" value="<?php echo $email ?>"> <?php endif; ?>

Upvotes: 1

Views: 5596

Answers (2)

Sankar V
Sankar V

Reputation: 4128

$this->customer->getEmail(); - this will get you logged in customer's email id.

In catalog/controller/checkout/success.php, insert the below code between

if (isset($this->session->data['order_id'])) {

and

$this->cart->clear();

Code to add:

if (isset($this->session->data['guest'])) { 
    $this->data['customer_email'] = $this->session->data['guest']['email'];  // Guest user's email
} elseif($this->customer->isLogged()) {
    $this->data['customer_email'] = $this->customer->getEmail(); // Customer's email
} 

Then in success.tpl, add:

<?php if(isset($customer_email)){ ?>
<input name="email" type="hidden" value="<?php echo $customer_email ?>">
<?php } ?>

Upvotes: 0

Jay Gilford
Jay Gilford

Reputation: 15151

To do this properly you need to open /catalog/controller/checkout/success.php and find this line of code

if (isset($this->session->data['order_id'])) {

After it, add the following

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

In your template /catalog/view/theme/your-theme-name/template/common/success.tpl you need to put

<?php if(!empty($email)) echo $email; ?>

Wherever you want to see the e-mail address

Upvotes: 3

Related Questions