如何在Opencart 2x的结帐成功页面上显示订单详细信息 [英] How to Show Order Details on Checkout Success Page in Opencart 2x

查看:196
本文介绍了如何在Opencart 2x的结帐成功页面上显示订单详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在成功页面上显示成功订单的订单详细信息,但无法这样做.这里的另一个答案建议修改success.php和success.tpl,但在Opencart 2上不起作用.

I am trying to show order details for successful orders on success page but unable to do so. Another answer here suggests to modify success.php and success.tpl but it's not working on Opencart 2.

我尝试了什么?

catalog/controller/checkout/success.php

catalog/controller/checkout/success.php

并在以下代码中添加了新行:

and added new lines in the following code:

public function index() {
$this->data['order_id'] = 0; // <-- NEW LINE
$this->data['total'] = 0; // <-- NEW LINE

if (isset($this->session->data['order_id'])) {
    $this->data['order_id'] = $this->session->data['order_id']; // <-- NEW LINE
    $this->data['total'] = $this->cart->getTotal(); // <-- NEW LINE

    $this->cart->clear();

    unset($this->session->data['shipping_method']);
    unset($this->session->data['shipping_methods']);
    unset($this->session->data['payment_method']);
    unset($this->session->data['payment_methods']);
    unset($this->session->data['guest']);
    unset($this->session->data['comment']);
    unset($this->session->data['order_id']);    
    unset($this->session->data['coupon']);
    unset($this->session->data['reward']);
    unset($this->session->data['voucher']);
    unset($this->session->data['vouchers']);
}   

$this->language->load('checkout/success');

现在将以下代码添加到success.tpl

Now added the following code into success.tpl

<?php if($order_id) { ?>
<script type="text/javascript">
// Some code here
arr.push([
    "create_order",
    {order_id: '<?php echo $order_id; ?>', sum: '<?php echo $total; ?>'}
]);

但是在成功页面上没有显示任何内容.上面的代码显示了订单ID和总金额,但我想显示该订单的所有详细信息,包括名称,地址,产品,总金额,运费等.就像在订单发票中一样.

But it doesn't show anything on success page. The above code is to show order ID and total but I want to show all details of order including name, address, products, total, shipping, etc. Just like in the order invoice.

任何帮助将不胜感激.谢谢

Any help will be appreciated. Thank you

推荐答案

我在2.0版之前的版本中,实际上是为会话ID设置了一个新的订单ID,因为我发现$this->session->data['order_id']不一致并且有时在用户到达ControllerCheckoutSuccess时变得不固定.

What I did on pre 2.0 versions was to actually set a new variable to the session for the order id as I found that $this->session->data['order_id'] wasn't consistent and sometimes was getting unset by the time the user reached ControllerCheckoutSuccess.

如果您想使用这种方法,请编辑您的catalog/model/checkout/order.php文件.在302行左右(在addOrderHistory方法内),您将看到脚本在哪里检查订单状态ID,以确定是否应完成订单.

If you'd like to use this approach, edit your catalog/model/checkout/order.php file. At or about line 302 (within the addOrderHistory method) you'll see where the script checks for order status ids to determine if it should complete the order.

在该语句中,将您选择的新会话变量设置为传入的订单ID,也许是$this->session->data['customer_order_id'] = $order_id

Within that statement, set a new session variable of your choice to the order id passed in, perhaps $this->session->data['customer_order_id'] = $order_id

现在您有了一个会话变量,您知道该变量将保持一致,因为您自己创建了该变量,而OpenCart不会将其弄乱.

Now you have a session variable that you know will remain consistent since you've created it yourself and OpenCart won't mess with it.

如果您发现会话订单ID IS 在2.1>中保持一致,那么不必担心,只需继续使用内置的默认会话订单ID变量即可.

If you're finding that the session order id IS remaining consistent in 2.1 > then don't worry about this, just go ahead and use the default session order id variable built in.

下一步将由您决定如何通过PHP或Ajax加载发票数据.我不建议使用Ajax,因为这可以通过浏览器开发人员工具进行操作,并可能暴露其他客户的信息.通过使用PHP和会话,您可以消除这种风险,因为随机的黑客将无法访问其他客户的会话.

The next step will be for you to decide how you want your invoice data loaded, via PHP or Ajax. I wouldn't recommend using Ajax as since this could be manipulated with browser developer tools and may expose other's customer's information. By using PHP and the session you eliminate this risk since a random hacker won't have access to another customer's session.

以下两个选项都需要:

打开catalog/controller/checkout/success.php

在将语言文件加载到索引方法之后,立即添加以下内容:

Right after the language file is loaded in your index method add the following:

$order_id = false;

// If NOT using the custom variable mentioned SKIP this
if (isset($this->session->data['customer_order_id'])) {
    $order_id = $this->session->data['customer_order_id'];
}

如果您使用的是会话数据中的烘焙订单ID,请在该语句中设置订单ID:

If you're using the baked in session data order id, set your order id within that statement:

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

    $order_id = $this->session->data['order_id'];

选项1:

将收据数据添加到结帐/成功.

Add receipt data to checkout/success.

查找此行:

$data['button_continue'] = $this->language->get('button_continue');

应该在77-84行或附近.

Should be around line 77-84 or thereabout.

在这里,您将加载并格式化所有收据信息.

Here you'll load up and format all your receipt info.

打开catalog/controller/account/order.php

在第108行,您会找到info方法.

On line 108 you'll find the info method.

从这里开始乐趣:P

在上述$data['button_continue'] = $this->language->get('button_continue');行之后,将该方法的所有相关信息复制到您的结帐成功控制器中.

Copy all the relevant info from that method into your checkout success controller just after the $data['button_continue'] = $this->language->get('button_continue'); line mentioned above.

您需要逐行浏览并对其进行调整,因为请记住,此链接是为已登录的客户设计的,因此您不希望获得退货或重新订购等链接.

You'll need to go through this line by line and tweak it because remember this is designed for logged in customers, so you won't want links for returns or reorders etc.

接下来,您将要制作一个新模板,因为common/success模板是通用模板,并且在各处使用.

Next you're going to want to make a new template because the common/success template is generic and used all over the place.

复制catalog/view/theme/(your theme)/template/common/success.tpl

至:catalog/view/theme/(your theme)/template/checkout/success.tpl

打开catalog/view/theme/default/template/account/order_info.tpl

您需要添加到成功模板中的表从第28行开始,扩展到第139行.如果使用其他主题,则需要自己进行设置.

The tables you'll need to add to your success template start on line 28 and extend to line 139. If you're using a different theme, you'll need to suss this out for yourself.

不要忘记将checkout/success控制器中模板的路径更改为新的checkout/success tpl文件.

Don't forget to change the path to your template in your checkout/success controller to your new checkout/success tpl file.

注意:

重要的是要记住,所有 SHOULD 都必须在修改包中完成,而 NOT 则要在您的核心文件中完成,但是我不知道您的情况,所以这取决于您决定.

It's important to remember that all this SHOULD be done in a modification package and NOT in your core files, but I don't know your situation so that's up to you to decide.

选项2:

创建您自己的模块.

Create your own module.

我认为自1.4版以来已为此系统构建了此选项,这是最好的选择.

在模块中创建新的控制器,我们称之为ControllerModuleReceipt:

Create new controller in modules, let's call it ControllerModuleReceipt:

<?php

/**
 * Controller class for displaying a receipt on checkout success.
 */
class ControllerModuleReceipt extends Controller
{
    /**
     * Replicates the ControllerAccountOrder::info
     * method for displaying order info in our
     * ControllerCheckoutSuccess::index method
     * 
     * @param  int $order_id   our order id
     * @return mixed           receipt view
     */
    public function index($setting)
    {
        $this->load->language('account/order');
        $this->load->model('account/order');

        if (empty($setting['order_id'])) {
            return;
        }

        $order_id = $setting['order_id'];

        $order_info = $this->model_account_order->getOrder($order_id);

        if ($order_info) {

            $data['text_order_detail']     = $this->language->get('text_order_detail');
            $data['text_invoice_no']       = $this->language->get('text_invoice_no');
            $data['text_order_id']         = $this->language->get('text_order_id');
            $data['text_date_added']       = $this->language->get('text_date_added');
            $data['text_shipping_method']  = $this->language->get('text_shipping_method');
            $data['text_shipping_address'] = $this->language->get('text_shipping_address');
            $data['text_payment_method']   = $this->language->get('text_payment_method');
            $data['text_payment_address']  = $this->language->get('text_payment_address');
            $data['text_history']          = $this->language->get('text_history');
            $data['text_comment']          = $this->language->get('text_comment');

            $data['column_name']           = $this->language->get('column_name');
            $data['column_model']          = $this->language->get('column_model');
            $data['column_quantity']       = $this->language->get('column_quantity');
            $data['column_price']          = $this->language->get('column_price');
            $data['column_total']          = $this->language->get('column_total');
            $data['column_action']         = $this->language->get('column_action');
            $data['column_date_added']     = $this->language->get('column_date_added');
            $data['column_status']         = $this->language->get('column_status');
            $data['column_comment']        = $this->language->get('column_comment');

            $data['invoice_no'] = '';

            if ($order_info['invoice_no']) {
                $data['invoice_no'] = $order_info['invoice_prefix'] . $order_info['invoice_no'];
            }

            $data['order_id']   = $order_id;
            $data['date_added'] = date($this->language->get('date_format_short'), strtotime($order_info['date_added']));

            $format = '{firstname} {lastname}' . "\n" . '{company}' . "\n" . '{address_1}' . "\n" . '{address_2}' . "\n" . '{city} {postcode}' . "\n" . '{zone}' . "\n" . '{country}';

            if ($order_info['payment_address_format']) {
                $format = $order_info['payment_address_format'];
            }

            $find = array(
                '{firstname}',
                '{lastname}',
                '{company}',
                '{address_1}',
                '{address_2}',
                '{city}',
                '{postcode}',
                '{zone}',
                '{zone_code}',
                '{country}'
            );

            $replace = array(
                'firstname' => $order_info['payment_firstname'],
                'lastname'  => $order_info['payment_lastname'],
                'company'   => $order_info['payment_company'],
                'address_1' => $order_info['payment_address_1'],
                'address_2' => $order_info['payment_address_2'],
                'city'      => $order_info['payment_city'],
                'postcode'  => $order_info['payment_postcode'],
                'zone'      => $order_info['payment_zone'],
                'zone_code' => $order_info['payment_zone_code'],
                'country'   => $order_info['payment_country']
            );

            $data['payment_address'] = str_replace(array("\r\n", "\r", "\n"), '<br />', preg_replace(array("/\s\s+/", "/\r\r+/", "/\n\n+/"), '<br />', trim(str_replace($find, $replace, $format))));

            $data['payment_method'] = $order_info['payment_method'];

            $format = '{firstname} {lastname}' . "\n" . '{company}' . "\n" . '{address_1}' . "\n" . '{address_2}' . "\n" . '{city} {postcode}' . "\n" . '{zone}' . "\n" . '{country}';

            if ($order_info['shipping_address_format']) {
                $format = $order_info['shipping_address_format'];
            }

            $find = array(
                '{firstname}',
                '{lastname}',
                '{company}',
                '{address_1}',
                '{address_2}',
                '{city}',
                '{postcode}',
                '{zone}',
                '{zone_code}',
                '{country}'
            );

            $replace = array(
                'firstname' => $order_info['shipping_firstname'],
                'lastname'  => $order_info['shipping_lastname'],
                'company'   => $order_info['shipping_company'],
                'address_1' => $order_info['shipping_address_1'],
                'address_2' => $order_info['shipping_address_2'],
                'city'      => $order_info['shipping_city'],
                'postcode'  => $order_info['shipping_postcode'],
                'zone'      => $order_info['shipping_zone'],
                'zone_code' => $order_info['shipping_zone_code'],
                'country'   => $order_info['shipping_country']
            );

            $data['shipping_address'] = str_replace(array("\r\n", "\r", "\n"), '<br />', preg_replace(array("/\s\s+/", "/\r\r+/", "/\n\n+/"), '<br />', trim(str_replace($find, $replace, $format))));

            $data['shipping_method'] = $order_info['shipping_method'];

            $this->load->model('catalog/product');
            $this->load->model('tool/upload');

            // Products
            $data['products'] = array();

            $products = $this->model_account_order->getOrderProducts($this->request->get['order_id']);

            foreach ($products as $product) {
                $option_data = array();

                $options = $this->model_account_order->getOrderOptions($this->request->get['order_id'], $product['order_product_id']);

                foreach ($options as $option) {
                    $value = false;

                    if ($option['type'] == 'file') {
                        $upload_info = $this->model_tool_upload->getUploadByCode($option['value']);

                        if ($upload_info) {
                            $value = $upload_info['name'];
                        }
                    }

                    if (! $value) {
                        $value = $option['value'];
                    }

                    $option_data[] = array(
                        'name'  => $option['name'],
                        'value' => (utf8_strlen($value) > 20 ? utf8_substr($value, 0, 20) . '..' : $value)
                    );
                }

                $product_info = $this->model_catalog_product->getProduct($product['product_id']);

                $data['products'][] = array(
                    'name'     => $product['name'],
                    'model'    => $product['model'],
                    'option'   => $option_data,
                    'quantity' => $product['quantity'],
                    'price'    => $this->currency->format($product['price'] + ($this->config->get('config_tax') ? $product['tax'] : 0), $order_info['currency_code'], $order_info['currency_value']),
                    'total'    => $this->currency->format($product['total'] + ($this->config->get('config_tax') ? ($product['tax'] * $product['quantity']) : 0), $order_info['currency_code'], $order_info['currency_value'])
                );
            }

            // Voucher
            $data['vouchers'] = array();

            $vouchers = $this->model_account_order->getOrderVouchers($this->request->get['order_id']);

            foreach ($vouchers as $voucher) {
                $data['vouchers'][] = array(
                    'description' => $voucher['description'],
                    'amount'      => $this->currency->format($voucher['amount'], $order_info['currency_code'], $order_info['currency_value'])
                );
            }

            // Totals
            $data['totals'] = array();

            $totals = $this->model_account_order->getOrderTotals($this->request->get['order_id']);

            foreach ($totals as $total) {
                $data['totals'][] = array(
                    'title' => $total['title'],
                    'text'  => $this->currency->format($total['value'], $order_info['currency_code'], $order_info['currency_value']),
                );
            }

            $data['comment'] = nl2br($order_info['comment']);

            if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/receipt.tpl')) {
                return $this->load->view($this->config->get('config_template') . '/template/module/receipt.tpl', $data);
            } else {
                return $this->load->view('default/template/module/receipt.tpl', $data);
            }
        }
    }
}

模板:

接下来,让我们在catalog/views/theme/default/module/receipt.tpl

<table class="table table-bordered table-hover">
  <thead>
    <tr>
      <td class="text-left" colspan="2"><?= $text_order_detail; ?></td>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td class="text-left" style="width: 50%;"><?php if ($invoice_no): ?>
        <b><?= $text_invoice_no; ?></b> <?= $invoice_no; ?><br />
        <?php endif; ?>
        <b><?= $text_order_id; ?></b> #<?= $order_id; ?><br />
        <b><?= $text_date_added; ?></b> <?= $date_added; ?></td>
      <td class="text-left"><?php if ($payment_method): ?>
        <b><?= $text_payment_method; ?></b> <?= $payment_method; ?><br />
        <?php endif; ?>
        <?php if ($shipping_method): ?>
        <b><?= $text_shipping_method; ?></b> <?= $shipping_method; ?>
        <?php endif; ?></td>
    </tr>
  </tbody>
</table>
<table class="table table-bordered table-hover">
  <thead>
    <tr>
      <td class="text-left" style="width: 50%;"><?= $text_payment_address; ?></td>
      <?php if ($shipping_address): ?>
      <td class="text-left"><?= $text_shipping_address; ?></td>
      <?php endif; ?>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td class="text-left"><?= $payment_address; ?></td>
      <?php if ($shipping_address): ?>
      <td class="text-left"><?= $shipping_address; ?></td>
      <?php endif; ?>
    </tr>
  </tbody>
</table>
<div class="table-responsive">
  <table class="table table-bordered table-hover">
    <thead>
      <tr>
        <td class="text-left"><?= $column_name; ?></td>
        <td class="text-left"><?= $column_model; ?></td>
        <td class="text-right"><?= $column_quantity; ?></td>
        <td class="text-right"><?= $column_price; ?></td>
        <td class="text-right"><?= $column_total; ?></td>
        <?php if ($products): ?>
        <td style="width: 20px;"></td>
        <?php endif; ?>
      </tr>
    </thead>
    <tbody>
      <?php foreach ($products as $product): ?>
      <tr>
        <td class="text-left"><?= $product['name']; ?>
          <?php foreach ($product['option'] as $option): ?>
          <br />
          &nbsp;<small> - <?= $option['name']; ?>: <?= $option['value']; ?></small>
          <?php endforeach; ?></td>
        <td class="text-left"><?= $product['model']; ?></td>
        <td class="text-right"><?= $product['quantity']; ?></td>
        <td class="text-right"><?= $product['price']; ?></td>
        <td class="text-right"><?= $product['total']; ?></td>
        <td class="text-right" style="white-space: nowrap;"><?php if ($product['reorder']): ?>
          <a href="<?= $product['reorder']; ?>" data-toggle="tooltip" title="<?= $button_reorder; ?>" class="btn btn-primary"><i class="fa fa-shopping-cart"></i></a>
          <?php endif; ?>
          <a href="<?= $product['return']; ?>" data-toggle="tooltip" title="<?= $button_return; ?>" class="btn btn-danger"><i class="fa fa-reply"></i></a></td>
      </tr>
      <?php endforeach; ?>
      <?php foreach ($vouchers as $voucher): ?>
      <tr>
        <td class="text-left"><?= $voucher['description']; ?></td>
        <td class="text-left"></td>
        <td class="text-right">1</td>
        <td class="text-right"><?= $voucher['amount']; ?></td>
        <td class="text-right"><?= $voucher['amount']; ?></td>
        <?php if ($products): ?>
        <td></td>
        <?php endif; ?>
      </tr>
      <?php endforeach; ?>
    </tbody>
    <tfoot>
      <?php foreach ($totals as $total): ?>
      <tr>
        <td colspan="3"></td>
        <td class="text-right"><b><?= $total['title']; ?></b></td>
        <td class="text-right"><?= $total['text']; ?></td>
        <?php if ($products): ?>
        <td></td>
        <?php endif; ?>
      </tr>
      <?php endforeach; ?>
    </tfoot>
  </table>
</div>
<?php if ($comment): ?>
<table class="table table-bordered table-hover">
  <thead>
    <tr>
      <td class="text-left"><?= $text_comment; ?></td>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td class="text-left"><?= $comment; ?></td>
    </tr>
  </tbody>
</table>
<?php endif; ?>

再次,如果使用您自己的主题,则需要对此进行调整.

Once again, if using your own theme you'll need to adjust this.

添加模块以检查成功

回到结帐成功控制器中,我们需要添加模块.

Back in the checkout success controller we need to add the module.

找到$data['content_bottom'] = $this->load->controller('common/content_bottom');

在该行之后添加以下内容:

After that line add this:

$data['receipt'] = false;
if ($order_id) {
    $data['receipt'] = $this->load->controller('module/receipt', array('order_id' => $order_id));
}

添加到成功模板

打开catalog/view/theme/default/common/success.tpl

<?php echo $text_message; ?>之后添加:

<?php if ($receipt): ?>
  <?= $receipt; ?>
<?php endif; ?>

应该就是这样.再一次,最好通过修改将更改添加到核心文件中,但是通过创建自己的模块,添加修改要容易得多,要处理的要少得多.

And that should be it. Once again it's best to add the changes to core files via a modification, but by creating your own module it's MUCH easier to add a modification, much less to deal with.

我还没有测试上面的代码,但是它应该可以工作或者错误最少.随时发布任何错误,我们将很乐意为您解决问题.

I haven't tested the code above but it should work or have minimal errors. Feel free to post any errors and I'll be happy to help fix them.

这篇关于如何在Opencart 2x的结帐成功页面上显示订单详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆