Перейти к содержанию

Fixing an Incorrect Countdown Timer in CS-Cart Full Page Cache


Рекомендуемые сообщения

Post #16796 Опубликовано

 

When using the **AlexBranding “Deal of the Day and Extended Promotions” add-on** together with the **CS-Commerce Full Page Cache add-on**, the promotion countdown timer may show an incorrect remaining time.

 

This issue occurs because the countdown value is generated on the server and then stored inside the cached HTML. When another visitor opens the same product page later, the timer starts with the old cached value instead of calculating the current remaining time.

 

## Tested Add-on Versions

This solution was tested with:

* AlexBranding Deal of the Day and Extended Promotions: **v3.10.5**
* CS-Commerce Full Page Cache: **v5.6.0**
* CS-Cart Ultimate: **4.18.3**
* AB UniTheme2
* HTML minification enabled

No modification to the Full Page Cache add-on is required.

## The Cause

The AlexBranding add-on calculates the remaining seconds when CS-Cart renders the page:

```smarty
{$total_seconds = $to_timestamp - $smarty.const.TIME}
```

It then writes this calculated value into the JavaScript:

```javascript
ab_dotd_js_counter(
    'ab__deal_of_the_day_{$block.block_id}',
    {$total_seconds},
    JSON.parse('{json_encode(fn_ab__dotd_get_time_units_plurals()) nofilter}')
);
```

For example, the first visitor may generate the page when 3,600 seconds remain. Full Page Cache stores this value.

If another visitor opens the page 15 minutes later, the cached HTML may still tell the counter to start at 3,600 seconds instead of the correct 2,700 seconds.

The correct approach is to cache only the fixed promotion end timestamp and calculate the remaining seconds in the visitor’s browser.

## File 1: `init_countdown.tpl`

Open:

```text
var/themes_repository/responsive/templates/addons/ab__deal_of_the_day/components/init_countdown.tpl
```

Find:

```smarty
{$total_seconds = $to_timestamp - $smarty.const.TIME}

{if $total_seconds}
```

Replace it with:

```smarty
{if $to_timestamp}
```

This prevents the server-generated remaining time from being used as the cached starting value.

## File 2: JavaScript Counter Template

Open:

```text
var/themes_repository/responsive/templates/addons/ab__deal_of_the_day/components/counters/javascript.tpl
```

Find:

```javascript
ab_dotd_js_counter('ab__deal_of_the_day_{$block.block_id}', {$total_seconds}, JSON.parse('{json_encode(fn_ab__dotd_get_time_units_plurals()) nofilter}'));
```

Replace it with:

```javascript
var total_seconds = Math.max(
    0,
    Number({$to_timestamp}) - Math.floor(Date.now() / 1000)
);

ab_dotd_js_counter(
    'ab__deal_of_the_day_{$block.block_id}',
    total_seconds,
    JSON.parse('{json_encode(fn_ab__dotd_get_time_units_plurals()) nofilter}')
);
```

The cached page now contains the absolute promotion end timestamp. Each visitor’s browser calculates the current remaining time when the page is opened.

## File 3: FlipClock Counter Template

If the add-on uses the FlipClock.js counter, open:

```text
var/themes_repository/responsive/templates/addons/ab__deal_of_the_day/components/counters/flipclock.tpl
```

Find:

```javascript
var total_seconds = Number({$total_seconds});
```

Replace it with:

```javascript
var total_seconds = Math.max(
    0,
    Number({$to_timestamp}) - Math.floor(Date.now() / 1000)
);
```

This applies the same cache-safe calculation to the FlipClock counter.

 

## Clear All Caches

After making the changes:

1. Clear the standard CS-Cart cache.
2. Completely clear the CS-Commerce Full Page Cache.
3. Open the product page in a private or incognito browser window.
4. Wait several minutes and open the same page in another private session.
5. Confirm that the second session shows a lower and correct remaining time.

## Result

Full Page Cache and HTML minification can remain enabled. No additional AJAX request is required, so the fix does not negatively affect page-loading performance.

The timer now works as follows:

```text
Remaining time = promotion end timestamp − current browser timestamp
```

Because the browser performs this calculation on every page visit, the countdown remains correct even when the surrounding HTML is served from Full Page Cache.

## Important Update Notice

A future update of the AlexBranding add-on may overwrite these modified templates. Keep a copy of the changes and verify the counter after every add-on update.

Ideally, this behavior should be implemented directly by the add-on developer, since absolute end timestamps are generally safer for countdown timers used on cached pages.

---
 

 

Для публикации сообщений создайте учётную запись или авторизуйтесь

Вы должны быть пользователем, чтобы оставить комментарий

Создать аккаунт

Зарегистрируйте новый аккаунт в нашем сообществе. Это очень просто!

Регистрация нового пользователя

Войти

Уже есть аккаунт? Войти в систему.

Войти
  • Последние посетители   0 пользователей онлайн

    • Ни одного зарегистрированного пользователя не просматривает данную страницу
×
×
  • Создать...