NenTang
Học tập · thực hành · tiến bộ mỗi ngày
21 / 22

Tạo trang web Bán hàng Thương mại điện tử Sử dụng PHP MySQL và render giao diện với template TWIG Chương 6 Bài 10

Chương 6 Bài 10. Thực hiện Trang Giỏ hàng

legacy-admin-baitap-1

1 thg 2, 2025

702 lượt xem

1 giờ 22 phút

Step 1: tạo file Logic hiển thị thông tin Giỏ hàng

  • Tạo file frontend/checkout/cart.php
php
1<?php
2// Include file cấu hình ban đầu của `Twig`
3require_once __DIR__ . '/../../bootstrap.php';
4 
5// Truy vấn database để lấy danh sách
6// 1. Include file cấu hình kết nối đến database, khởi tạo kết nối $conn
7include_once(__DIR__ . '/../../dbconnect.php');
8 
9// Kiểm tra dữ liệu trong session
10$data = [];
11if (isset($_SESSION['giohangdata'])) {
12 $data = $_SESSION['giohangdata'];
13} else {
14 $data = [];
15}
16 
17// Yêu cầu `Twig` vẽ giao diện được viết trong file `frontend/thanhtoan/giohang.html.twig`
18// với dữ liệu truyền vào file giao diện được đặt tên là `giohangdata`
19// dd($data);
20echo $twig->render('frontend/thanhtoan/giohang.html.twig', ['giohangdata' => $data]);

Step 2: tạo file template giao diện Giỏ hàng

  • Tạo file templates/frontend/checkout/giohang.html.twig
php
1{# Kế thừa layout frontend #}
2{% extends "frontend/layouts/layout.html.twig" %}
3 
4{# Nội dung trong block title #}
5{% block title %}
6Giỏ hàng
7{% endblock %}
8{# End Nội dung trong block title #}
9 
10{# Nội dung trong block headline #}
11{% block headline %}
12Giỏ hàng
13{% endblock %}
14{# End Nội dung trong block headline #}
15 
16{% block customstyles %}
17<style>
18 .hinhdaidien {
19 width: 150px;
20 height: 150px;
21 }
22</style>
23{% endblock %}
24 
25{# Nội dung trong block content #}
26{% block content %}
27<div class="container mt-4">
28 <div id="errors-container" class="alert alert-danger d-none face" role="alert">
29 <button type="button" class="close" data-dismiss="alert" aria-label="Close">
30 <span aria-hidden="true">&times;</span>
31 </button>
32 </div>
33 
34 <h1 class="text-center">Giỏ hàng</h1>
35 <div class="row">
36 <div class="col col-md-12">
37 {% if giohangdata %}
38 <table class="table table-bordered">
39 <thead>
40 <tr>
41 <th>STT</th>
42 <th>Ảnh đại diện</th>
43 <th>Tên sản phẩm</th>
44 <th>Số lượng</th>
45 <th>Đơn giá</th>
46 <th>Thành tiền</th>
47 <th>Hành động</th>
48 </tr>
49 </thead>
50 <tbody id="datarow">
51 {% for sanpham in giohangdata %}
52 <tr>
53 <td>{{ loop.index }}</td>
54 <td>
55 {% if sanpham.hinhdaidien %}
56 <img src="/project-nentang/assets/uploads/{{ sanpham.hinhdaidien }}" class="hinhdaidien" />
57 {% else %}
58 <img src="/project-nentang/assets/shared/img/default-image_600.png" class="hinhdaidien" />
59 {% endif %}
60 </td>
61 <td>{{ sanpham.sp_ten }}</td>
62 <td>
63 <input type="number" id="soluong_{{ sanpham.sp_ma }}" name="soluong" value="{{ sanpham.soluong }}" />
64 <button class="btn btn-primary btn-sm btn-capnhat-soluong" data-sp-ma="{{ sanpham.sp_ma }}">Cập nhật</button>
65 </td>
66 <td>{{ sanpham.gia }}</td>
67 <td>{{ sanpham.soluong * sanpham.gia }}</td>
68 <td>
69 <!-- Nút xóa, bấm vào sẽ xóa thông tin dựa vào khóa chính `sp_ma` -->
70 <a id="delete_{{ loop.index }}" data-sp-ma="{{ sanpham.sp_ma }}" class="btn btn-danger btn-delete-sanpham">
71 <i class="fa fa-trash" aria-hidden="true"></i> Xóa
72 </a>
73 </td>
74 </tr>
75 {% endfor %}
76 </tbody>
77 </table>
78 {% else %}
79 <h2>Giỏ hàng rỗng!!!</h2>
80 {% endif %}
81 
82 <a href="/" class="btn btn-warning btn-md"><i class="fa fa-arrow-left" aria-hidden="true"></i>&nbsp;Quay
83 về trang chủ</a>
84 <a href="/frontend/thanhtoan/thanhtoan" class="btn btn-primary btn-md"><i class="fa fa-shopping-cart"
85 aria-hidden="true"></i>&nbsp;Thanh toán</a>
86 </div>
87 </div>
88</div>
89{% endblock %}
90{# End Nội dung trong block content #}
91 
92{% block customscripts %}
93<script>
94 $(document).ready(function () {
95 function removeSanPhamVaoGioHang(id) {
96 var dulieugoi = {
97 sp_ma: id
98 };
99 $.ajax({
100 url: '/project-nentang/frontend/ajax/giohang-xoasanpham-ajax.php',
101 method: "POST",
102 dataType: 'json',
103 data: dulieugoi,
104 success: function (data) {
105 // Refresh lại trang
106 location.reload();
107 },
108 error: function (jqXHR, textStatus, errorThrown) {
109 console.log(textStatus, errorThrown);
110 var htmlString = `<h1>Không thể xử lý</h1>`;
111 $('#thongbao').html(htmlString);
112 // Hiện thông báo
113 $('.alert').removeClass('d-none').addClass('show');
114 }
115 });
116 };
117 $('.btn-delete-sanpham').on('click', function (event) {
118 event.preventDefault();
119 
120 var id = $(this).data('sp-ma');
121
122 console.log(id);
123 removeSanPhamVaoGioHang(id);
124 });
125 
126 
127 function capnhatSanPhamTrongGioHang(id, soluong) {
128 var dulieugoi = {
129 sp_ma: id,
130 soluong: soluong
131 };
132 $.ajax({
133 url: '/project-nentang/frontend/ajax/giohang-capnhatsanpham-ajax.php',
134 method: "POST",
135 dataType: 'json',
136 data: dulieugoi,
137 success: function (data) {
138 // Refresh lại trang
139 location.reload();
140 },
141 error: function (jqXHR, textStatus, errorThrown) {
142 console.log(textStatus, errorThrown);
143 var htmlString = `<h1>Không thể xử lý</h1>`;
144 $('#thongbao').html(htmlString);
145 // Hiện thông báo
146 $('.alert').removeClass('d-none').addClass('show');
147 }
148 });
149 };
150 $('.btn-capnhat-soluong').on('click', function(event) {
151 event.preventDefault();
152 
153 var id = $(this).data('sp-ma');
154 var soluongmoi = $('#soluong_' + id).val();
155 
156 capnhatSanPhamTrongGioHang(id, soluongmoi);
157 });
158 });
159</script>
160{% endblock %}