Add to Cart on Stock Level

Description

07.26c09v1 Add to Cart on Stock Level

#1. Install Code

#1.1. Click Gear icon on Store Page

07.26c05v1 Variant Description

Next, click Advanced > Page Header Code Injection

07.26c05v1 Variant Description

#1.2. Add this code to right box

<!-- 07.26c09v1 Add to Cart on Stock Level -->
<script>
const stockConfig = {
  unlimitedText: "Pre-Order"
};
</script>
<script>
window.addEventListener("DOMContentLoaded", function() {
  fetch(window.location.pathname + '?format=json')
    .then(function(response) {
      return response.json();
    })
    .then(function(data) {
      if (data && data.item && data.item.variants) {
        let isUnlimited = false;
        for (let i = 0; i < data.item.variants.length; i++) {
          if (data.item.variants[i].unlimited) {
            isUnlimited = true;
            break;
          }
        }
        if (isUnlimited) {
          const checkElement = setInterval(function() {
            const textElement = document.querySelector('.add-to-cart-text');
            if (textElement) {
              textElement.textContent = stockConfig.unlimitedText;
              clearInterval(checkElement);
            }
          }, 100);
          setTimeout(function() { clearInterval(checkElement); }, 5000);
        }
      }
    })
    .catch(function(error) {});
});
</script>

07.26c09v1 Add to Cart on Stock Level

#2. Customize

#2.1. To change Pre-Order text, change this line 04

unlimitedText: "Pre-Order"

 

Buy me a coffee