Đôi lúc bạn được yêu cầu viết thêm những sản phẩm đã xem cho theme WordPress đặc biệt là Flatsome . Hôm nay mình giới thiệu tới các bạn cách add short những sản phẩm đã xem nhé !
Code sản phẩm đã xem cho theme flatsome wordpress

bắt tay vào việc nào
CÓ THỂ BẠN QUAN TÂM
- Tặng 3 free domaind.SPACE hoàn toàn miễn phí
- Code cố định một Widget(Stick Widget) tự động trượt khi lăn chuột
- Code hiển thị số bình luận trong Flatsome WordPress
- Hover hiển thị thông tin sản phẩm dạng Tooltip cho Woocommerce
- Tạo thêm sidebar và get sidebar trong wordpress
- Code lấy ảnh đầu tiên trong bài viết làm ảnh đại diện Auto Post Thumbnail
Chèn code bên dưới vào function.php của theme (ưu tiên child theme nếu có nhé).
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 |
function isures_set_user_visited_product_cookie() { if (!is_singular('product')) { return; } global $post; if (empty($_COOKIE['woocommerce_recently_viewed'])) { $viewed_products = array(); } else { $viewed_products = wp_parse_id_list((array) explode('|', wp_unslash($_COOKIE['woocommerce_recently_viewed']))); } $keys = array_flip($viewed_products); if (isset($keys[$post->ID])) { unset($viewed_products[$keys[$post->ID]]); } $viewed_products[] = $post->ID; if (count($viewed_products) > 22) { array_shift($viewed_products); } wc_setcookie('woocommerce_recently_viewed', implode('|', $viewed_products)); } add_action('wp', 'isures_set_user_visited_product_cookie'); |
Với code trên thì đã xử lý được phần cookie rồi. Tiếp tục là khởi tạo shortcode
Khởi tạo shortcode sản phẩm đã xem
Code sau cũng bỏ vào function.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 |
add_shortcode('isures_recently_viewed_products', 'isures_2718_prod_viewed_atts'); function isures_2718_prod_viewed_atts() { ob_start(); $viewed_products = !empty($_COOKIE['woocommerce_recently_viewed']) ? (array) explode('|', wp_unslash($_COOKIE['woocommerce_recently_viewed'])) : array(); $viewed_products = array_reverse(array_filter(array_map('absint', $viewed_products))); ?> <div id="isures-recently--wrap"> <div class="isures-container"> <?php if (!empty($viewed_products)) { echo do_shortcode('[products type="row" limit="8" columns="6" ids="' . implode(',', $viewed_products) . '"]'); } else { echo 'Không có sản phẩm xem gần đây'; } ?> </div> </div> <?php return ob_get_clean(); } |
Columns ở dòng số 18 code trên:
- Limit=”8″ có nghĩa là hiển thị tối qua 8 sản phẩm. -> thay số tùy bạn.
- Columns=”6″ số cột là 6. -> thay tùy ý.
Hiển thị shortcode sản phẩm đã xem
Tiếp theo để hiển thị được sản phẩm đã xem thì bạn thêm shortcode sau vào nơi cần hiển thị, Sidebar hoặc chỗ nào có thể add html là có thể add short nhé !
1 |
[visited_product] |
Oke như thế là đã xong 1 tính năng sản phẩm đã xem cho website của bạn rồi. Chúc thành công.!
Nguồn bài đăng : https://thietkewebgiarehcm.com/tao-san-pham-da-xem/