Hi @faecom1
To add a countdown timer to the announcement bar in Shopify’s Dawn theme, follow these steps:
1-Access the Theme Editor:
2-Add Custom Code:
- In the theme editor, click on the Theme settings tab.
- Scroll down and select Custom CSS.
- Paste the following CSS code to style the countdown timer:
.announcement-bar__message {
display: flex;
justify-content: center;
align-items: center;
}
.countdown-timer {
margin-left: 10px;
font-weight: bold;
}
- Next, click on the Custom JavaScript section.
- Paste the following JavaScript code to implement the countdown functionality:
document.addEventListener(‘DOMContentLoaded’, function() {
function initializeCountdown(endTime) {
function updateCountdown() {
const now = new Date().getTime();
const distance = endTime - now;
if (distance < 0) {
clearInterval(interval);
document.querySelector(‘.countdown-timer’).innerHTML = ‘EXPIRED’;
return;
}
const hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
const minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
const seconds = Math.floor((distance % (1000 * 60)) / 1000);
document.querySelector(‘.countdown-timer’).innerHTML =
hours + 'h ’ + minutes + 'm ’ + seconds + 's ';
}
updateCountdown();
const interval = setInterval(updateCountdown, 1000);
}
const now = new Date();
const resetTime = new Date();
resetTime.setHours(24, 0, 0, 0); // Reset at midnight
if (now > resetTime) {
resetTime.setDate(resetTime.getDate() + 1);
}
const announcementBar = document.querySelector(‘.announcement-bar__message’);
if (announcementBar) {
const timerSpan = document.createElement(‘span’);
timerSpan.className = ‘countdown-timer’;
announcementBar.appendChild(timerSpan);
initializeCountdown(resetTime.getTime());
}
});
3-Save and Preview:
- Click Save to apply the changes.
- Preview your store to ensure the countdown timer appears correctly in the announcement bar.
This code sets a countdown timer that resets daily at midnight, displaying the remaining hours, minutes, and seconds in the announcement bar. Ensure that your theme’s announcement bar is enabled and visible for the timer to display properly.
If the timer doesn’t appear as expected, double-check that the CSS and JavaScript code snippets are correctly placed in their respective sections and that there are no syntax errors.