OpenCart 2.0.1.1中如何将成功页控制器数据传递至视图模板?
Fixing "Notice: Undefined index: order_id" in OpenCart 2.0.1.1 Success Page
Hey there, let's break down why you're seeing this error and how to fix it step by step.
Why the Error Happens
That "Undefined index: order_id" notice pops up because the order_id key doesn't exist in your session data. Here's the common scenario in OpenCart 2.0.1.1:
- By default, the success page controller clears the
order_idfrom the session after loading order details (to prevent duplicate submissions or unintended access). - If you refresh the success page, or your custom controller isn't handling the session correctly, the
order_idis gone, leading to the error.
Step-by-Step Fix
1. Update the Controller (success.php)
First, we need to add safe checks to retrieve the order ID—either from the session (if it's still available) or use a cookie as a fallback for when the session is cleared. Here's the revised code:
$this->load->model('checkout/order'); // Safely get order ID from session first if (isset($this->session->data['order_id'])) { $order_id = $this->session->data['order_id']; $data['orderDetails'] = $this->model_checkout_order->getOrder($order_id); // Store order ID in cookie as a fallback for page refreshes setcookie('recent_order_id', $order_id, time() + 3600, '/'); // Optional: Keep this if you want to follow OpenCart's default session cleanup unset($this->session->data['order_id']); } else { // Fallback to cookie if session has no order ID $order_id = isset($_COOKIE['recent_order_id']) ? (int)$_COOKIE['recent_order_id'] : 0; if ($order_id) { $data['orderDetails'] = $this->model_checkout_order->getOrder($order_id); } else { // Redirect to home if no order ID found anywhere $this->response->redirect($this->url->link('common/home')); } } $this->response->setOutput($this->load->view('default/template/common/success.tpl', $data));
2. Fix the View (success.tpl)
Your current view tries to echo the entire $orderDetails array, which will just show "Array" on the page. Instead, loop through the array to display meaningful details. Here's an improved version:
<div id="content" class="<?php echo $class; ?>"><?php echo $content_top; ?> <h1><?php echo 'Order Details'; ?></h1> <?php if (isset($orderDetails) && !empty($orderDetails)) { ?> <div class="order-details"> <p><strong>Order ID:</strong> <?php echo $orderDetails['order_id']; ?></p> <p><strong>Order Total:</strong> <?php echo $this->currency->format($orderDetails['total'], $orderDetails['currency_code'], $orderDetails['currency_value']); ?></p> <p><strong>Order Status:</strong> <?php echo $orderDetails['status']; ?></p> <p><strong>Date Ordered:</strong> <?php echo date('Y-m-d H:i:s', strtotime($orderDetails['date_added'])); ?></p> <!-- Add more fields like customer info, shipping address, etc., as needed --> </div> <?php } else { ?> <p>Sorry, no order details could be found.</p> <?php } ?> </div>
Extra Tips
- Security Note: Always cast
order_idto an integer (like(int)$_COOKIE['recent_order_id']) to prevent potential SQL injection risks. - Avoid Session Reliance: If you're working with a payment gateway callback, consider passing the order ID via a secure parameter instead of relying solely on sessions—some gateways might redirect in a way that resets the session.
- Cleanup Cookies: If you use the cookie fallback, expire it after a short time (like 1 hour) to avoid storing unnecessary data.
内容的提问来源于stack exchange,提问作者Raj
相关产品推荐
相关产品推荐

