Forked from meetKowshik/Showing smart shopping bar on Category, Product, Cart page, Checkout page in BigCommerce
Created
October 8, 2019 10:57
-
-
Save caot/6a30bac29c9e042d789d9f8e4e3563fb to your computer and use it in GitHub Desktop.
Showing smart shopping bar on Category, Product and Cart page
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
First add the below html code where you want to show in the category.html, product.html and cart.html | |
<div class="shipping-bar-wrapper" data-coupon-code="{{lang 'freeDelivery'}}"></div> | |
Add the freeDevlivery(the coupon amount) text to the en.json file in the lang file. | |
Add this Js code to global.js in the assets/js/theme folder | |
fetch('/api/storefront/cart', { | |
credentials: 'same-origin' | |
}) | |
.then(function(response) { | |
return response.json(); | |
}) | |
.then (function(data) { | |
if(data.length === 0) { | |
var couponAmount = Number(document.querySelector('.shipping-bar-wrapper').getAttribute('data-coupon-code')); | |
document.querySelector('.shipping-bar-wrapper').innerHTML = `<p class="smart-shipping-bar">Get Free Shipping when you spend $${couponAmount} or more</p>`; | |
} else { | |
var cartAmount = data[0].cartAmount; | |
var couponCount = Number(document.querySelector('.shipping-bar-wrapper').getAttribute('data-coupon-code')); | |
if(cartAmount > couponCount) { | |
document.querySelector('.shipping-bar-wrapper').innerHTML = `<p class="smart-shipping-bar">You’ve qualified for FREE SHIPPING!</p>`; | |
} else { | |
var leftAmount = couponCount - cartAmount; | |
document.querySelector('.shipping-bar-wrapper').innerHTML = `<p class="smart-shipping-bar">After product added: Spend just $${leftAmount} more to earn free shipping</p>`; | |
} | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment