Last active
April 6, 2020 19:42
-
-
Save ryandejaegher/5ccc7e6a7baf24b534759fc9a2ddd71a to your computer and use it in GitHub Desktop.
This is a plugin for Squarespace to add a minimumPriceQuantity option for checkout
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
| // REQUIREMENTS | |
| // User should be able to set the minimum required price | |
| // User should be able to adjust the error message | |
| // User should be able to change the currency symbol to match their country | |
| // The error message should be temporarily displayed if they are below the order minimum | |
| // The error message should display the required minimum amount | |
| // The error message should disappear once they are above the order minimum | |
| // The error message should appear again if they are below the minimum order amount | |
| var minimumPricePlugin = (function () { | |
| // | |
| // Variables | |
| // | |
| var publicAPIs= {}; | |
| var settings; | |
| // Default settings for plugin | |
| var defaults = { | |
| checkoutSelector: '[class*="checkout-button"]', | |
| cartSubTotalSelector: '[class*="subtotalPrice"]', | |
| currencySymbol: '$', | |
| minimumPrice: 50, | |
| errorMessage() { return `Sorry you're below the minimum order amount ${this.currencySymbol}${this.minimumPrice}` } | |
| }; | |
| // | |
| // Methods | |
| // | |
| // This checks the current cart total and lets you pass an argument for your minimum price | |
| publicAPIs.minimumPriceCheck = function(minimumPrice) { | |
| var checkoutButton = document.querySelector(settings.checkoutSelector); | |
| // Get subtotal | |
| var cartSubTotal = document.querySelector(settings.cartSubTotalSelector); | |
| var cartTotalString = cartSubTotal.textContent; | |
| var cartTotalNum = parseFloat(cartTotalString.replace(/[^\d.-]/g,'')); | |
| if (cartTotalNum < minimumPrice) { | |
| checkoutButton.disabled = true; | |
| checkoutButton.style.pointerEvents = 'none'; | |
| checkoutButton.style.cursor = 'not-allowed'; | |
| showErrorMessage(); | |
| console.log('too low') | |
| } else { | |
| checkoutButton.disabled = false; | |
| checkoutButton.style.pointerEvents = 'auto'; | |
| checkoutButton.style.cursor = 'pointer'; | |
| hideErrorMessage(); | |
| console.log('good to go!'); | |
| } | |
| } | |
| // Create Error Message | |
| var addErrorMessage = function (sibling) { | |
| sibling.insertAdjacentHTML('afterend', `<div class="errorMessage">${settings.errorMessage()}</div>`); | |
| } | |
| // Show Error Message | |
| var showErrorMessage = function() { | |
| var errorMessage = document.querySelector('.errorMessage'); | |
| errorMessage.style.display = 'block'; | |
| } | |
| // Hide Error Message | |
| var hideErrorMessage = function() { | |
| var errorMessage = document.querySelector('.errorMessage'); | |
| errorMessage.style.display = 'none'; | |
| } | |
| // | |
| // Inits & Event Listeners | |
| // | |
| // Initializes the plugin @param {Object} options | |
| publicAPIs.init = function(options) { | |
| // Creates a new settings object from the defaults and user options object | |
| settings = Object.assign({}, defaults, options); | |
| // Get checkout button | |
| var checkoutButton = document.querySelector(settings.checkoutSelector); | |
| // Get subtotal | |
| var cartSubTotal = document.querySelector(settings.cartSubTotalSelector); | |
| console.log(cartSubTotal) | |
| var minimumPrice = settings.minimumPrice; | |
| // Adds error HTML | |
| addErrorMessage(checkoutButton); | |
| // This is the function that the Mutation Observer will run | |
| var mutationCallback = mutations => { | |
| publicAPIs.minimumPriceCheck(settings.minimumPrice) | |
| console.log("mutations",mutations) | |
| } | |
| // Creates new mutation observer | |
| var observer = new MutationObserver(mutationCallback); | |
| // Mutation Observer configuration | |
| var mutationConfig = { | |
| attributes:true, | |
| characterData:true, | |
| subtree:true | |
| }; | |
| observer.observe(cartSubTotal, mutationConfig); | |
| publicAPIs.minimumPriceCheck(settings.minimumPrice); | |
| }; | |
| // | |
| // Return Public APIs | |
| // | |
| return publicAPIs; | |
| })(); | |
| // Initialize plugin | |
| minimumPricePlugin.init(); | |
| // TODO Ensure that it only watches the checkout page | |
| // String replace method and regex to keep float numbers i.e. $12.49 -> "12.49" | |
| // var s = "-12345.50 €".replace(/[^\d.-]/g, ''); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment