Tại sao nên thêm slider sản phẩm đã xem?
1. Tăng thời gian người dùng ở lại website
Khi khách truy cập thấy lại những sản phẩm họ đã xem trước đó, họ sẽ có xu hướng quay lại, khám phá thêm hoặc hoàn tất mua hàng.
2. Cải thiện trải nghiệm mua sắm
Slider sản phẩm đã xem giúp khách hàng dễ dàng tiếp cận lại các sản phẩm yêu thích mà không cần phải tìm kiếm từ đầu.
Hướng dẫn tích hợp slider sản phẩm đã xem

Thêm code vào file functions.php
Để bắt đầu, thêm đoạn code dưới đây vào file functions.php trong theme WordPress của bạn.
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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | // Bắt đầu session để sử dụng session cho lưu trữ dữ liệu if (!session_id()) { session_start(); } // Shortcode for Viewed Products Slider function viewed_products_slider_shortcode($atts) { $atts = shortcode_atts( array( 'limit' => 10, // Number of products to show ), $atts, 'viewed_products_slider' ); // Kiểm tra nếu có session lưu trữ các sản phẩm đã xem $viewed_products = isset($_SESSION['viewed_products']) ? $_SESSION['viewed_products'] : array(); if (empty($viewed_products)) { return '<p>Bạn chưa xem sản phẩm nào.</p>'; } // Lấy sản phẩm từ database dựa trên danh sách các sản phẩm đã xem $args = array( 'post_type' => 'product', 'posts_per_page' => intval($atts['limit']), 'post__in' => $viewed_products, 'orderby' => 'post__in', ); $viewed_query = new WP_Query($args); if (!$viewed_query->have_posts()) return '<p>Bạn chưa xem sản phẩm nào.</p>'; ob_start(); ?> <style> /* Styles for the viewed products slider */ .viewed-products-slider .product-slide { display: flex; flex-direction: column; justify-content: space-between; align-items: center; padding: 10px; border: 1px solid #eee; background: #fff; text-align: center; margin: 3px; overflow: hidden; border-radius: 10px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); transition: transform 0.3s ease, box-shadow 0.3s ease; position: relative; } .viewed-products-slider .product-slide:hover { transform: translateY(-5px); box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2); } /* Add a similar badge for "Sản phẩm đã xem" */ .viewed-products-slider .viewed-label { position: absolute; top: 10px; left: 10px; background: #ff0000; /* Red background */ color: #fff; padding: 5px; font-size: 12px; font-weight: bold; border-radius: 5px; } /* Other viewed products styles */ .viewed-products-slider .box-image { height: 300px; /* Fixed height to match flash-sale slider */ overflow: hidden; } .viewed-products-slider .box-image img { width: 100%; transition: transform 5s ease; /* Image slide effect */ } .viewed-products-slider .box-image:hover img { transform: translateY(-50%); /* Image slides up */ } .viewed-products-slider h3 { font-size: 16px; height: 40px; overflow: hidden; -webkit-line-clamp: 2; display: -webkit-box; -webkit-box-orient: vertical; } .viewed-products-slider .price { margin-top: auto; font-size: 18px; color: #333; } .viewed-products-slider .thongtin { display: flex; justify-content: center; gap: 10px; } .viewed-products-slider .nut { background-color: #ffa800; color: #fff; border-radius: 5px; text-decoration: none; transition: background-color 0.3s; } .viewed-products-slider .nut:hover { background-color: #005a87; } /* Responsive design */ @media (max-width: 1024px) { .viewed-products-slider .product-slide { width: 100%; /* Ensure product occupies full width */ } } @media (max-width: 768px) { .viewed-products-slider .product-slide { width: 45%; /* Adjust width for smaller screens */ } } @media (max-width: 480px) { .viewed-products-slider .product-slide { width: 100%; /* Ensure product occupies full width on very small screens */ } } /* Label for "Sản phẩm đã xem" */ .viewed-products-slider .viewed-label { position: absolute; top: 10px; left: 10px; background: #ff0000; /* Red background for the label */ color: #fff; padding: 5px; font-size: 12px; font-weight: bold; border-radius: 5px; z-index: 2; /* Ensure label is above the product */ } </style> <div class="viewed-products-slider"> <?php while ($viewed_query->have_posts()) : $viewed_query->the_post(); global $product; ?> <div class="product-slide"> <div class="viewed-label">Sản phẩm đã xem</div> <a href="<?php the_permalink(); ?>"> <div class="box-image"><?php echo woocommerce_get_product_thumbnail(); ?></div> <h3><?php the_title(); ?></h3> <div class="price"><?php echo $product->get_price_html(); ?></div> </a> <div class="thongtin"> <a class="xemdemo nut" href="<?php the_permalink(); ?>">Xem Demo</a> <a class="xemchitiet nut" href="<?php the_permalink(); ?>">Xem chi tiết</a> </div> </div> <?php endwhile; ?> </div> <script> jQuery(document).ready(function($) { // Initialize the slider for viewed products $('.viewed-products-slider').slick({ slidesToShow: 4, slidesToScroll: 1, autoplay: true, autoplaySpeed: 2000, arrows: true, dots: true, responsive: [ { breakpoint: 768, settings: { slidesToShow: 2, slidesToScroll: 1, dots: false } }, { breakpoint: 480, settings: { slidesToShow: 1, slidesToScroll: 1, dots: false } } ] }); }); </script> <?php wp_reset_postdata(); ?> <?php return ob_get_clean(); } add_shortcode('viewed_products_slider', 'viewed_products_slider_shortcode'); // Hàm để thêm sản phẩm vào danh sách sản phẩm đã xem trong session function add_product_to_viewed() { if (is_product()) { // Lấy ID sản phẩm hiện tại $product_id = get_the_ID(); // Lấy danh sách sản phẩm đã xem từ session if (!isset($_SESSION['viewed_products'])) { $_SESSION['viewed_products'] = array(); } // Thêm sản phẩm vào danh sách nếu chưa có if (!in_array($product_id, $_SESSION['viewed_products'])) { $_SESSION['viewed_products'][] = $product_id; } } } add_action('template_redirect', 'add_product_to_viewed'); |
Lưu ý khi sử dụng
Kiểm tra khả năng tương thích
Đảm bảo kiểm tra chức năng slider trên các thiết bị khác nhau như điện thoại, máy tính bảng và máy tính bàn để mang lại trải nghiệm tốt nhất.
Sử dụng caching hợp lý
Để tránh ảnh hưởng đến tốc độ tải trang, bạn nên tối ưu hóa caching khi lưu trữ thông tin sản phẩm đã xem.
Kết luận
Tính năng slider sản phẩm đã xem không chỉ nâng cao trải nghiệm người dùng mà còn giúp tăng tỷ lệ chuyển đổi cho website. Với hướng dẫn chi tiết trên, bạn có thể dễ dàng tích hợp tính năng này vào WordPress. Thử ngay và cảm nhận sự khác biệt!
- Hướng Dẫn Tạo Trang Chức Năng Đổi Mật Khẩu Tùy Chỉnh Cho Thành Viên WordPress
- Sharecode Maxton – Mẫu Bảng Điều Khiển Quản Trị Bootstrap 5 Đa Năng và Chuyên Nghiệp
- Cách Vô Hiệu Hóa Lazy Load Hình Ảnh Mặc Định Trong WordPress 5.5
- #1 Hướng Dẫn Code Chức Năng Flash Sale WooCommerce
- Sharecode Theme Marketing Chuẩn SEO: BrandBuzz – Cực Đỉnh Cho Dân Làm Digital!