Trong bài viết này, mình sẽ hướng dẫn bạn cách tạo mã giảm giá trên WordPress bằng cách sử dụng Advanced Custom Fields (ACF) mà không cần cài đặt thêm plugin. Đây là một phương pháp gọn nhẹ, đơn giản và giúp tối ưu hóa tốc độ của website. Bài viết này sẽ hướng dẫn từng bước cách tạo và hiển thị mã giảm giá trên website của bạn.

Yêu Cầu Trước Khi Bắt Đầu
- Đảm bảo rằng plugin Advanced Custom Fields (ACF) đã được cài đặt và kích hoạt.
- Một chút kiến thức cơ bản về PHP và WordPress để tùy chỉnh theo nhu cầu.
Tải ACF pro: Tải Miễn Phí Plugin ACF Pro Phiên Bản Mới Nhất 3.7
Hướng Dẫn Tạo Mã Giảm Giá Bằng ACF
1. Tạo Trang Quản Lý Mã Giảm Giá
Đầu tiên, bạn cần thêm trang tùy chọn cho mã giảm giá bằng cách sử dụng đoạn mã sau trong file functions.php của theme:
|
1 2 3 4 5 6 7 8 9 10 11 |
// Đảm bảo rằng ACF đã được cài đặt và kích hoạt if (function_exists('acf_add_options_page')) { // Thêm trang tùy chọn acf_add_options_page(array( 'page_title' => 'Quản Lý Mã Giảm Giá', 'menu_title' => 'Mã Giảm Giá', 'menu_slug' => 'manage_coupons', 'capability' => 'manage_options', 'redirect' => false, )); } |
2. Thêm Các Trường ACF
Sau khi đã tạo trang tùy chọn, bạn cần thêm các trường cần thiết để nhập dữ liệu mã giảm giá. Chèn đoạn mã dưới đây vào functions.php:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
// Thêm các trường ACF add_action('acf/init', function() { if (function_exists('acf_add_local_field_group')) { acf_add_local_field_group(array( 'key' => 'group_coupons', 'title' => 'Mã Giảm Giá', 'fields' => array( array( 'key' => 'field_coupon_list', 'label' => 'Danh Sách Mã Giảm Giá', 'name' => 'coupon_list', 'type' => 'repeater', 'sub_fields' => array( array( 'key' => 'field_coupon_code', 'label' => 'Mã Giảm Giá', 'name' => 'coupon_code', 'type' => 'text', ), array( 'key' => 'field_coupon_condition', 'label' => 'Điều Kiện', 'name' => 'coupon_condition', 'type' => 'text', ), array( 'key' => 'field_coupon_image', 'label' => 'Hình Ảnh', 'name' => 'coupon_image', 'type' => 'image', 'return_format' => 'url', ), array( 'key' => 'field_coupon_display', 'label' => 'Hiển Thị', 'name' => 'coupon_display', 'type' => 'true_false', 'ui' => 1, 'default_value' => 1, ), ), 'min' => 0, 'layout' => 'table', ), ), 'location' => array( array( array( 'param' => 'options_page', 'operator' => '==', 'value' => 'manage_coupons', ), ), ), )); } }); |
Sau khi tạo, chúng ta sẽ có một trang như sau:

3. Tạo Shortcode Để Hiển Thị Mã Giảm Giá
Sau khi tạo xong các trường tùy chỉnh, bạn cần tạo shortcode để hiển thị mã giảm giá trên trang web. Thêm đoạn mã sau vào functions.php:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
// Shortcode để hiển thị mã giảm giá function muathemewpgiare_coupon_shortcode_with_image() { $coupons = get_field('coupon_list', 'options') ?: []; // Lấy mã giảm giá từ options ob_start(); ?> <div class="coupon-list"> <?php foreach ($coupons as $coupon) : ?> <?php if ($coupon['coupon_display']) : // Kiểm tra trạng thái hiển thị ?> <div class="coupon-item"> <img class="coupon-image" src="<?php echo esc_url($coupon['coupon_image']); ?>" alt="Coupon Image"> <div class="coupon-content"> <strong><?php echo esc_html($coupon['coupon_code']); ?></strong> <p><?php echo esc_html($coupon['coupon_condition']); ?></p> <button class="copy-btn" data-code="<?php echo esc_attr($coupon['coupon_code']); ?>" onclick="copyCouponCode(this, '<?php echo esc_attr($coupon['coupon_code']); ?>')">Sao chép mã</button> <button class="detail-btn" onclick="showCouponDetail('<?php echo esc_html($coupon['coupon_code']); ?>', '<?php echo esc_html($coupon['coupon_condition']); ?>')">Chi tiết</button> </div> </div> <?php endif; ?> <?php endforeach; ?> </div> <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script> <script type="text/javascript"> function copyCouponCode(button, code) { navigator.clipboard.writeText(code).then(function() { button.textContent = "Đã sao chép"; button.style.backgroundColor = "#28a745"; button.style.color = "#fff"; Swal.fire({ icon: 'success', title: 'Sao chép thành công!', text: 'Mã giảm giá ' + code + ' đã được sao chép.', timer: 2000, showConfirmButton: false }); setTimeout(function() { button.textContent = "Sao chép mã"; button.style.backgroundColor = "#0344ff"; button.style.color = "#fff"; }, 10000); }, function(err) { console.error('Không thể sao chép mã giảm giá', err); }); } function showCouponDetail(code, condition) { Swal.fire({ title: 'Chi tiết mã giảm giá', html: '<strong>Mã:</strong> ' + code + '<br><strong>Điều kiện:</strong> ' + condition, icon: 'info', confirmButtonText: 'Đóng' }); } </script> <style> .coupon-list { display: flex; flex-direction: column; gap: 15px; } .coupon-item { display: flex; align-items: center; background-color: #f9f9f9; border-radius: 8px; padding: 10px; box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1); } .coupon-image { width: 60px; height: 60px; object-fit: cover; border-radius: 8px; margin-right: 15px; } .coupon-content { flex-grow: 1; } .coupon-content strong { display: block; font-size: 16px; color: #333; margin-bottom: 5px; } .coupon-content p { font-size: 14px; color: #777; margin-bottom: 10px; } .copy-btn, .detail-btn { background-color: #0344ff; color: #fff; border: none; padding: 4px 7px; border-radius: 5px; cursor: pointer; font-size: 14px; margin-right: 5px; } .copy-btn:focus, .detail-btn:focus { outline: none; } </style> <?php return ob_get_clean(); // Trả về nội dung của shortcode } add_shortcode('muathemewpgiare_coupons', 'muathemewpgiare_coupon_shortcode_with_image'); |
4. Sử Dụng Shortcode
Sau khi thêm shortcode vào file functions.php, bạn chỉ cần thêm shortcode dưới vào bài viết hoặc trang để hiển thị danh sách mã giảm giá.
|
1 |
[muathemewpgiare_coupons] |

Theme Wordpress Dịch Vụ Chụp Ảnh Chuyên Nghiệp
Theme wordpress bán đồ công nghệ điện tử
Theme Wordpress Cửa Hàng Thời Trang Nam Nữ
Theme Wordpress Bán Mỹ Phẩm Làm Đẹp
Theme wordpress giới thiệu công ty bds 43
Theme wordpress giới thiệu ca sỷ
Theme wordpress shop đồ gia dụng tổng hợp
theme wordpress giới thiệu công ty xây dựng
Theme wordpress phòng khám đông y
Theme wordpress nhà hàng tiệc cưới
Theme wordpress dịch vụ bảo vệ
Theme wordpress siêu thị mẹ và bé giá rẻ
Theme wordpress giống tiktokshop
Theme wordpress bán thiết bị di động điện thoại, laptop cực đẹp
Theme Wordpress Marketing Truyền Thông
Theme wordpress nội thất bàn ghế văn phòng
Theme wordpress bán oto hyundai 1
Theme wordpress dự án khu đô thị thương mại
Theme wordpress dịch vụ nấu tiệc cưới tại nhà
Theme wordpress thiết bị máy nén
Theme Wordpress Phòng Khám giá rẻ
Theme worpress thiết kế thi công nội thất giá rẻ
Theme wordpress nội thất 02 chuẩn seo
Theme wordpress bán thiết bị phòng cháy chữa cháy
Theme wordpress công ty kế toán
Theme wordpress lading pape khóa học
Landing pape bán khóa học đầu tư doanh nhân
Theme WordPress trung tâm anh ngữ
Theme wordpress bán thuốc nam
Theme wordpress thời trang 19
Theme wordpress siêu thị điện máy, điện lạnh giá rẻ chuẩn seo
Theme wordpress khóa học chuẩn SEO cực xịn
Theme WordPress công ty Agency 2
Theme wordpress giao diện bán hàng gạch, keo ốp lát tường
Theme wordpress blog về cây xanh
Theme WordPress dịch vụ sửa nhà
Theme wordpress review quảng cáo game
Theme WordPress Trường Anh ngữ
Theme wordpress nhôm kính 3
Theme WordPress trường học edukid
Theme wordpress công ty cảm biến
Theme wordpress công ty cẩu trục
Theme wordpress bất động sản 26
Theme Wordpress Shop Thời Trang Đồ Lót nữ
Theme wordpress bán xe vinfast 2
Theme wordpress bán hàng thực phẩm chức năng 5
Theme wordpress nhà xe dịch vụ
Theme wordpress bán văn phòng phẩm giá rẻ
Theme wordpress mỹ phẩm cực đẹp, chuẩn seo giá rẻ
Theme wordpress thiết kế web theo mẩu giá rẻ
Theme wordpress công ty cơ khí chế tạo
Theme wordpress công ty thiết kế ,maketing
Theme wordpress du học chuẩn đẹp
Theme wordpress công ty nội thất HNK
Theme WordPress Dịch Vụ Vận Tải 3 – Thiết Kế Website Vận Tải Chuyên Nghiệp, Hiện Đại
Theme wordpress bán hàng thực phẩm chức năng 4
Theme Wordpress Giao Diện Thời Trang03 Giá Rẻ
Theme wordpress bất động sản 27
Theme wordpress bán quần áo giá rẻ
Theme wordpress công ty truyền thông media
Theme wordpress bán xe hyundai 4
Theme Wordpress Bán Thuốc Và Thực Phẩm Chức Năng
Theme WordPress công ty thi công & lắp đặt Cửa Tự Động
Theme wordpress bán gạch và thiết bị vệ sinh 3
Theme wordpress công ty xây dựng 4
Theme wordpress giới thiệu cơ sở massage ,spa
Theme wordpress dịch vụ bảo vệ 1 giá rẻ