XIMRX
XIMRX

Reputation: 2170

How can we use order details on checkout success page of Opencart Template?

I need to add order data in OpenCart 2.1.0.1 template on checkout/success page. I added the below code in view/theme/name/template/common/success.tpl but it is not showing order data, (values are empty because there is no order id in $this->session->data).

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

In /catalog/controller/checkout/success.php I have defined these PHP variables:

$this->data['orderid'] = $$this->session->data['order_id'];
$this->data['fname'] = $this->session->data['guest']['firstname'];
$this->data['lname'] = $this->session->data['guest']['lastname'];

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

Can anyone guide what am I missing and why these values are not loading in.tpl file, and what is the solution. Even hard coded values are also not retrieved in .tpl.

Values of $this->session->data:

(
[language] => en
[currency] => USD
[user_id] => 9
[token] => 5ZiNOGeVjCdg4gefNkDLcHzF1zMUVKgA
[account] => guest
[payment_address] => Array
    (
        [firstname] => sdfsdf
        [lastname] => adfafa
        [company] => 
        [address_1] => test test test
        [address_2] => 
        [postcode] => 34324
        [city] => sdfsdfd
        [country_id] => 216
        [zone_id] => 3396
        [country] => Turkmenistan
        [iso_code_2] => TM
        [iso_code_3] => TKM
        [address_format] => 
        [custom_field] => Array
            (
            )
        [zone] => Ahal Welayaty
        [zone_code] => A
    )
[shipping_address] => Array
    (
        [firstname] => sdfsdf
        [lastname] => adfafa
        [company] => 
        [address_1] => test test test
        [address_2] => 
        [postcode] => 34324
        [city] => sdfsdfd
        [country_id] => 216
        [zone_id] => 3396
        [country] => Turkmenistan
        [iso_code_2] => TM
        [iso_code_3] => TKM
        [address_format] => 
        [zone] => Ahal Welayaty
        [zone_code] => A
        [custom_field] => Array
            (
            )
    )
)

Upvotes: 0

Views: 2044

Answers (2)

You Old Fool
You Old Fool

Reputation: 22931

Correct variable name

Opencart 2.0 and up doesn't use $this->data for templates. Instead simply use $data. SO instead of:

$this->data['hardcoded'] = "HARD CODED";

You can write:

$data['hardcoded'] = "HARD CODED";

Position of code

All order data is cleared at the top of the controller so if you need to access it make sure you define those variables before:

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

Efficiency & Accuracy of data

Normally, $this->session->data['guest'] is only defined if customer is not logged in. The best way to use order data would be to get data from the order itself and assign it to a single array since all the conditionals have already been dealt with when it was created. This saves you the trouble of re-defining each variable and checking logged in status, etc:

$this->load->model('checkout/order');
$data['order'] = $this->model_checkout_order->getOrder($this->session->data['order_id']);

Then in your tpl you can access anything about the order in the $order array:

$order['order_id']
$order['firstname']

Upvotes: 3

Gary Baldwin
Gary Baldwin

Reputation: 1

is the $data['order'] bit right in

$this->load->model('checkout/order');
$data['order'] = $this->model_checkout_order->getOrder($this->session->data['order_id']);

should it not be?

$this->load->model('checkout/order');
$order = $this->model_checkout_order->getOrder($this->session->data['order_id']);

Upvotes: 0

Related Questions