Personalized checkout and custom promotions with Shopify Scripts
Hi everyone, 😀
I’ve been troubleshooting an issue where the “Not Secure” warning sometimes appears on our mobile site. This has only recently started happening and seems to coincide with our Add to Cart (ATC) button freezing up — preventing any further interaction on the page after hitting ATC.
Everything on our domain looks normal, but I suspect the issue may stem from this shipping date calculator code (see below). I’m also wondering if this code is optimised. Could it be contributing to the “Not Secure” error and freezing the ATC button?
Here’s the script:
<script>
document.addEventListener("DOMContentLoaded", function() {
try {
var today = new Date();
var options = { timeZone: 'Australia/Sydney', hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: false };
var timeString = today.toLocaleTimeString('en-AU', options);
var hour = parseInt(timeString.split(':')[0], 10);
var day = today.getDay();
var nextShippingDay = new Date(today);
var shippingText = "Ships by ";
if (hour >= 12) {
if (day >= 1 && day <= 4) {
nextShippingDay.setDate(today.getDate() + 1);
} else if (day === 5) {
nextShippingDay.setDate(today.getDate() + 3);
} else if (day === 6) {
nextShippingDay.setDate(today.getDate() + 2);
} else {
nextShippingDay.setDate(today.getDate() + 1);
}
} else {
shippingText = "Ships today, ";
}
// Format the date to a readable string
var dateOptions = { weekday: 'short', month: 'long', day: 'numeric', timeZone: "Australia/Sydney" };
var formattedDate = nextShippingDay.toLocaleDateString('en-AU', dateOptions);
document.getElementById('shipping-date').innerHTML = '<span class="shipping-text">' + shippingText + '</span><span class="shipping-date">' + formattedDate + '</span>';
} catch (error) {
document.getElementById('shipping-date').innerHTML = '<span class="shipping-text">Invalid Date</span>';
}
});
</script>
Any insights would be much appreciated. Thanks in advance!
Thanks to everyone who participated in our AMA with 2H Media: Marketing Your Shopify St...
By Jacqui Sep 6, 2024The Hydrogen Visual Editor is now available to merchants in Shopify Editions | Summer '...
By JasonH Sep 2, 2024Note: Customizing your CSS requires some familiarity with CSS and HTML. Before you cust...
By JasonH Aug 12, 2024