Ngu ngơ học làm web (x38) - CakePHP2 - routes có tham số, lịch sử mua hàng

Tiếp theo của: Ngu ngơ học làm web (x37) - CakePHP2 – findAllById, TimeFormat, json_decode 
-----

Phần x38. CakePHP2 – routes có tham số, lịch sử mua hàng


Xem (clip số 39b – chickenrainshop):

- Routes có kèm id ví dụ /abc/:id (39b) (1":49)

- Sử dụng loadModel khi truy vấn trong controller (6":56)

Viết chức năng xem chi tiết một đơn hàng.

Tạo link đi đến trang ‘xem chi tiết một đơn hàng’:

[Views\Orders\history.ctp]

<?php else: ?>
<span class="label">Hủy</span>               
<?php endif ?>
</td>
<td><?php echo $this->Html->link('Xem', '/don-hang/'.$order['Order']['id']); ?></td>
</tr>
<?php endforeach ?>

Viết route cho link ‘/don-hang/’ .$order['Order']['id']

[routes.php]

Router::connect('/cap-nhat-thong-tin', ['controller' => 'users', 'action' => 'changeInfo']);
Router::connect('/don-hang/:id', ['controller' => 'orders', 'action' => 'detail'], ['pass'=>['id']]);
Router::connect('/lich-su-mua-hang', ['controller' => 'orders', 'action' => 'history']);

Viết action detail() trong OrdersController.php

[OrdersController.php]

/**
 * xem chi tiết đơn hàng
 *
 */

            public function detail($id = null) {
                        $order = $this->Order->findById($id);
                        if (!empty($order)) {
                                    $userInfo = $this->getUser();
                                    if ($userInfo['id'] == $order['Order']['user_id']) {
                                                $this->set('order', $order);
                                                // cho phép người dùng tải về các cuốn sách họ đã thanh toán
                                                if ($order['Order']['status'] == 1) {
                                                            $books = json_decode($order['Order']['order_info']);
                                                            $this->loadModel('Book');
                                                            foreach ($books as $book) {
                                                                        $this->Book->recursive = -1;
                                                                        $result = $this->Book->findById($book->id);
                                                                        $link[$book->id] = $result['Book']['link_download'];
                                                            }
                                                            $this->set('link', $link);
                                                }
                                    }
                        }
                        $this->set('title_for_layout', 'Xem chi tiết đơn hàng');
            }

Viết mã cho view detail:

[detail.ctp]

<div class="panel panel-info">
            <h4 class="panel-heading"><span class="glyphicon glyphicon-th"></span> Chi tiết đơn hàng</h4>
            <?php if (isset($order)): ?>
                        <?php
                                    $customer_info = json_decode($order['Order']['customer_info']);
                                    $payment_info = json_decode($order['Order']['payment_info']);
                                    $order_info = json_decode($order['Order']['orders_info']);
                        ?>
            <div class="row">
                        <div class="col col-lg-6">
                                    <p><strong>Họ tên người mua hàng: </strong><span><?php echo $customer_info->name; ?></span></p>
                                    <p><strong>Email: </strong><span><?php echo $customer_info->email; ?></span></p>
                                    <p><strong>Điện thoại: </strong><span><?php echo $customer_info->phone; ?></span></p>
                                    <p><strong>Địa chỉ: </strong><span><?php echo $customer_info->address; ?></span></p>
                        </div>
                        <div class="col col-lg-6">
                                    <p><strong>Mã đơn hàng: </strong><span><?php echo $order['Order']['id']; ?></span></p>
                                    <p><strong>Tổng cộng: </strong><span><?php echo $this->Number->currency($payment_info->total, 'VND', ['places'=>0, 'wholePosition'=>'after']); ?></span></p>
                                    <?php if (isset($payment_info->coupon)): ?>
                                                <p><strong>Mã giảm giá: </strong>
                                                            <span>
                                                                        <?php echo $payment_info->coupon; ?> -
                                                                        <strong>Giảm: </strong><?php echo $payment_info->discount; ?>%
                                                            </span>
                                                </p>
                                                <p><strong>Tiền phải trả: </strong><span><?php echo $this->Number->currency($payment_info->pay, 'VND', ['places'=>0, 'wholePosition'=>'after']); ?></span></p>
                                    <?php else: ?>
                                                <p><strong>Tiền phải trả: </strong><span><?php echo $this->Number->currency($payment_info->total, 'VND', ['places'=>0, 'wholePosition'=>'after']); ?></span></p>
                                    <?php endif ?>
                        </div>
            </div>
            <hr>
            Tình trạng đơn hàng:
            <?php if ($order['Order']['status'] == 0): ?>
                        <span class="label label-info">Đang xử lý</span>
            <?php elseif ($order['Order']['status'] == 1): ?>
                        <span class="label label-success">Đã xử lý</span>
            <?php else: ?>
                        <span class="label">Hủy</span>               
            <?php endif ?>
            <hr>
            <h4>Sách đã mua</h4>
            <table class="table table-striped">
                        <thead>
                                    <tr>
                                                <th>STT</th>
                                                <th>Tên sách</th>
                                                <th>Số lượng</th>
                                                <th>Giá</th>
                                                <th>Tải về</th>
                                    </tr>
                        </thead>
                        <tbody>
                                    <?php $i = 1; ?>
                                    <?php foreach ($order_info as $book): ?>
                                                <tr>
                                                            <td><?php echo $i++; ?></td>
                                                            <td><?php $book->title; ?></td>
                                                            <td><?php echo $book->quantity; ?></td>
                                                            <td><?php echo $book->sale_price; ?></td>
                                                            <td>
                                                                        <?php if (isset($link)): ?>
                                                                                    <?php echo $this->Html-link('<i class="glyphicon glyphicon-download"></i> Tải về', $link[$book->id], ['escape' => false]); ?>
                                                                        <?php else: ?>
                                                                                    Chưa xử lý     
                                                                        <?php endif ?>
                                                            </td>
                                                </tr>
                                    <?php endforeach ?>
                        </tbody>
            </table>
<?php else: ?>
            Đơn hàng này không tồn tại!
<?php endif ?>

</div>
-----------
Cập nhật 15/7/2017
-----------
Xem thêm:
Tổng hợp các bài viết về Ngu ngơ học làm web