Hướng dẫn reset thời gian element Countdown trong flatsome đơn giản

Linh 18-11-2025 18-11-2025 274 lượt xem

Bạn đã gặp trường hợp người đặt thời gian đếm ngược với element Count down thì nếu để thời gian đếm quá lâu thì không mang lại sự thu hút cho người mua, nhưng khi đặt thời gian quá ngắn thì phải thường xuyên cập nhật thời gian quá tốn thời gian
Hôm nay LiberC sẽ hướng dẫn bạn cách đặt thời gian tự động cho element Countdown trong flatsome.

Tìm hiểu về Element Countdown trong flatsome?

Countdown là một trong những tính năng quan trọng của Flatsome, giúp tạo hiệu ứng đếm ngược cho flash sale, chương trình khuyến mãi hoặc sự kiện giới hạn thời gian. Tuy nhiên, script countdown mặc định của uxbuilder Flatsome không hỗ trợ việc tự động lặp lại thời gian khi hết hạn (auto renew). Điều này khiến nhiều quản trị viên gặp khó khi chạy các chiến dịch theo chu kỳ, ví dụ: cứ mỗi tháng 1 lần hoặc mỗi tuần 1 lần.

Cách hoạt động của đoạn code

Mục tiêu: thay thế file flatsome-countdown-theme-js trong parent theme bằng file JS tùy chỉnh trong child theme.

  • Tạo lại file ux-countdown-custom.js trong child theme
  • Đăng ký lại flatsome-countdown-theme-js trong functions.php

Cách thực hiện:

Tạo file ux-countdown-custom.js mới

example.com/wp-content/themes/<flatsome-child>/inc/shortcodes/ux-countdown-custom.js
nội dùng file:

Flatsome.behavior(‘ux-countdown’, {
 attach: function (context) {
  jQuery(‘[data-countdown]’, context).each(function () {
   var $this = jQuery(this), finalDate = jQuery(this).data(‘countdown’);
   var t_hour = jQuery(this).data(‘text-hour’),
     t_min = jQuery(this).data(‘text-min’),
     t_week = jQuery(this).data(‘text-week’),
     t_day = jQuery(this).data(‘text-day’),
     t_sec = jQuery(this).data(‘text-sec’),
     t_min_p = jQuery(this).data(‘text-min-p’),
     t_hour_p = jQuery(this).data(‘text-hour-p’),
     t_week_p = jQuery(this).data(‘text-week-p’),
     t_day_p = jQuery(this).data(‘text-day-p’),
     t_sec_p = jQuery(this).data(‘text-sec-p’),
     t_plural = jQuery(this).data(‘text-plural’);
     var hours_plural = t_hour+t_plural;
     var days_plural = t_day+t_plural;
     var weeks_plural = t_week+t_plural;
     var min_plural = t_min;
     var sec_plural = t_sec;
     if(t_hour_p) hours_plural = t_hour_p;
     if(t_min_p) min_plural = t_min_p;
     if(t_week_p) weeks_plural = t_week_p;
     if(t_day_p) days_plural = t_day_p;
     if(t_sec_p) sec_plural = t_sec_p;
     var targetTime = new Date(finalDate);
      var currentTime = new Date();
      while (targetTime < currentTime) {
        targetTime.setMonth(targetTime.getMonth() + 1);
        const year = targetTime.getFullYear();
        const month = targetTime.getMonth() + 1; // Tháng bắt đầu từ 0, nên cộng thêm 1
        const day = targetTime.getDate();
        const hours = targetTime.getHours();
        const minutes = targetTime.getMinutes();
        finalDate = `${year}/${month}/${day} ${hours}:${minutes}`;
      }
     $this.countdown(finalDate).on(‘update.countdown’, function (event) {
          var format = ‘<span>%-H<strong>%!H:’+t_hour+’,’+hours_plural+’;</strong></span><span>%-M<strong>%!M:’+t_min+’,’+min_plural+’;</strong></span><span>%-S<strong>%!S:’+t_sec+’,’+sec_plural+’;</strong></span>’;
          if(event.offset.days > 0) { format = ‘<span>%-d<strong>%!d:’+t_day+’,’+days_plural+’;</strong></span>’ + format; }
          if(event.offset.weeks > 0) { format = ‘<span>%-w<strong>%!w:’+t_week+’,’+weeks_plural+’;</strong></span>’ + format; }
        jQuery(this).html(event.strftime(format));
     }).on(‘finish.countdown’, function (event) {
        var format = ‘<span>%-H<strong>%!H:’+t_hour+’,’+hours_plural+’;</strong></span><span>%-M<strong>%!M:’+t_min+’,’+min_plural+’;</strong></span><span>%-S<strong>%!S:’+t_sec+’,’+sec_plural+’;</strong></span>’;
        jQuery(this).html(event.strftime(format));
     });
  });
 }
});

Thêm code vào file functions.php

example.com/wp-content/themes/<flatsome-child>/functions.php
nội dùng thêm:

add_action(‘after_setup_theme’, function () {
    // Gọi file init shortcodes
    require_once get_stylesheet_directory() . ‘/inc/shortcodes/init.php’;
    // Ghi đè và đăng ký lại countdown của Flatsome
    add_action(‘wp_enqueue_scripts’, function () {
        wp_deregister_script(‘flatsome-countdown-theme-js’);
        wp_register_script(
            ‘flatsome-countdown-theme-js’,
            get_stylesheet_directory_uri() . ‘/inc/shortcodes/ux-countdown-custom.js’,
            array(‘flatsome-js’),
            ‘1.0.0’,
            true
        );
    }, 20);
}, 20);

Kết quả cuối cùng:

Thời gian set là thời gian cũ
Thời gian được đặt là thời gian trong quá khứ
Giao diện bên ngoài bên ngoài vẫn là đang được chạy bình thường