I'm currently working on implementing a shipping progress bar on my Shopify Theme. However, I've encountered an issue where the progress bar does not update correctly unless I refresh the page along with the content text.
Can anyone provide guidance or assistance with this project?
For a live demonstration, you can view it here:
The placement of my progress bar is above the add to cart button. https://i.sstatic.net/KO1ne.png
JavaScript Code:
var $freeShippingClass = $('.js-free-shipping'),
$freeShippingTextClass = $('.js-free-shipping-text'),
$free_fisrt = $freeShippingClass.attr('data-start'),
$free_end = $freeShippingClass.attr('data-end')
minOrderValue = parseInt($freeShippingClass.data('value')) || 0,
$percentClass = $('.js-free-shipping .progress-bar');
function generate(cart){
var priceCart = cart.total_price;
if (priceCart >= minOrderValue){
$percentClass.css('width','100%').removeClass('progress-bar-striped bg-primary');
$freeShippingTextClass.text(theme.strings.freeShipping)
}else{
let percent = priceCart / minOrderValue * 100;
let left = Shopify.formatMoney((minOrderValue - priceCart),theme.moneyFormat);
$percentClass.css('width',percent+'%').addClass('progress-bar-striped primary');
$freeShippingTextClass.html($free_fisrt +' '+ left +' '+ $free_end);
theme.updateCurrencies();
}
}
Shopify.getCart(function(cart){
generate(cart);
});
return {
load:generate
}
})()
Shipping.liquid
<div class="progress-bar progress-bar-striped progress-bar-animated" role="progressbar" aria-valuemin="0" aria-valuemax="100"></div>
<span class="free-shipping-content">
<span class="js-free-shipping-text">
</span>
</span>
</div>
CSS
.js-free-shipping{
position: relative;
height: 30px;
margin:0 0 15px;
background:#343a40;
border-radius:0;
font-size: val(--g-font-size);
#jsCrosssell &{
margin-bottom:0;
.progress-bar{
-webkit-animation:none;
animation:none;
}
}
.mini-cart-content &{
.progress-bar{
-webkit-animation:none;
animation:none;
}
}
}
.js-free-shipping-text{
.mini-cart-content &{
font-size:calc(val(--g-font-size) - 2px);
}
}
.mini-cart-content{
.js-free-shipping{
margin-top:15px;
}
}
.free-shipping-content{
position: absolute;
width: 100%;
text-align: center;
line-height: 30px;
color: white;
font-weight: bold;
left:0;
.svg,[class^=svg-]{
margin:-2px 10px 0 0 ;
}
}